Flexible application of POJ 3662 telephone Lines SPFA algorithm

Source: Internet
Author: User

Test instructions

to a graph with n nodes, which requires a path from 1 to n, you can make the K bar free, the cost of the path is the maximum of the length of the remaining edge of the path, and now requires the minimum value to be spent.

Ideas:

This problem can first think of the maximum value of the binary enumeration path, I think with SPFA more concise. The essence of SPFA is a search algorithm, and since it is a search, it involves the transfer of state. In the SPFA algorithm, which is generally the shortest path, when the node U is reached, the following transfer is required for E (U,V): if (D[v]>d[u]+w (e)) D[v]=d[u]+w (e). In the general case, to the junction U, to E (u,v) need to do a variety of transfer, such as the question to consider let e free and do not let e free two cases, specific please refer to the implementation.

Code:

POJ 3662//sepnine#include <iostream> #include <queue>using namespace std;const int Maxn=1024;const int Maxm=10024;struct edge{int V,w,next;} Edge[maxm*2];struct node{int u,used;}; int n,p,k,e;int head[maxn],dis[maxn][maxn],vis[maxn][maxn];void SPFA (int s,int t,int k) {queue<node> Q;memset ( Vis,0,sizeof (VIS)); int i,j;for (i=1;i<=n;++i) for (j=0;j<=k;++j) Dis[i][j]=int_max; Node x;x.u=s;x.used=0;dis[s][0]=0;vis[s][0]=1; Q.push (x); while (! Q.empty ()) {Node x=q.front (); int u=x.u,used=x.used; Q.pop (); vis[u][used]=0;for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v,w=edge[i].w;if (used+1<=k& &dis[v][used+1]>dis[u][used]) {//Let this edge free, free number of sides used to +1dis[v][used+1]=dis[u][used];if (vis[v][used+1]==0) { Node x;x.u=v;x.used=used+1;vis[x.u][x.used]=1; Q.push (x);}} if (Dis[v][used]>max (dis[u][used],w)) {//Do not let this edge free, dis[v][used] is dis[u][used] and W is the maximum value Dis[v][used]=max (dis[u][used ],W); if (vis[v][used]==0) {Node x;x.u=v;x.used=used;vis[x.u][x.used]=1; Q.push (x);}}} return;} int main () {scanf("%d%d%d", &n,&p,&k); E=0;memset (Head,-1,sizeof (head)); while (p--) {int a,b,c;scanf ("%d%d%d", &a,&b,&c); edge[e].v=b;edge[e].w=c,edge[e].next=head[a];head[a]= e++;edge[e].v=a;edge[e].w=c;edge[e].next=head[b];head[b]=e++;} SPFA (1,n,k), int ans=int_max;for (int i=0;i<=k;++i) ans=min (Ans,dis[n][i]), if (Ans==int_max) printf ("-1");  elseprintf ("%d", ans); return 0;}


Flexible application of POJ 3662 telephone Lines SPFA algorithm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.