标签:
最短路0 0
Sample Output2
SPFA算法SPFA比较简单,这篇文章写的不错
//#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<string> #include<queue> #include<cstring> using namespace std; template<class T>inline T read(T&x) { char c; while((c=getchar())<=32)if(c==EOF)return -1; bool ok=false; if(c=='-')ok=true,c=getchar(); for(x=0; c>32; c=getchar()) x=x*10+c-'0'; if(ok)x=-x; return 1; } template<class T> inline T read_(T&x,T&y) { return read(x)!=-1&&read(y)!=-1; } template<class T> inline T read__(T&x,T&y,T&z) { return read(x)!=-1&&read(y)!=-1&&read(z)!=-1; } template<class T> inline void write(T x) { if(x<0)putchar('-'),x=-x; if(x<10)putchar(x+'0'); else write(x/10),putchar(x%10+'0'); } template<class T>inline void writeln(T x) { write(x); putchar('\n'); } //-------ZCC IO template------ const int maxn=101; const double inf=999999999; #define lson (rt<<1),L,M #define rson (rt<<1|1),M+1,R #define M ((L+R)>>1) #define For(i,t,n) for(int i=(t);i<(n);i++) typedef long long LL; typedef double DB; #define bug printf("---\n"); #define mod 10007 int G[maxn][maxn]; bool vis[maxn]; int d[maxn]; int V,E; void spfa(int s) { For(i,0,V+1) { vis[i]=false; d[i]=inf; } d[s]=0; queue<int> q; q.push(s); vis[s]=true; while(!q.empty()) { int tmp=q.front();q.pop(); for(int u=1;u<=V;u++) { if(d[u]>d[tmp]+G[tmp][u]) { d[u]=d[tmp]+G[tmp][u]; if(!vis[u]) { q.push(u); vis[u]=true; } } } vis[tmp]=false; } } int main() { //#ifndef ONLINE_JUDGE //freopen("in.txt","r",stdin); // #endif // ONLINE_JUDGE int n,m,i,j,k,t; while(read_(V,E)&&(V||E)) { For(i,1,V+1)For(j,1,V+1)G[i][j]=inf; for(i=0;i<E;i++) { int a,b,c; read__(a,b,c); G[a][b]=c; G[b][a]=c; } spfa(1); writeln(d[V]); } return 0; }bell_man 时间O(V*E)
//#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<string> #include<cstring> using namespace std; template<class T>inline T read(T&x) { char c; while((c=getchar())<=32)if(c==EOF)return -1; bool ok=false; if(c=='-')ok=true,c=getchar(); for(x=0; c>32; c=getchar()) x=x*10+c-'0'; if(ok)x=-x; return 1; } template<class T> inline T read_(T&x,T&y) { return read(x)!=-1&&read(y)!=-1; } template<class T> inline T read__(T&x,T&y,T&z) { return read(x)!=-1&&read(y)!=-1&&read(z)!=-1; } template<class T> inline void write(T x) { if(x<0)putchar('-'),x=-x; if(x<10)putchar(x+'0'); else write(x/10),putchar(x%10+'0'); } template<class T>inline void writeln(T x) { write(x); putchar('\n'); } //-------ZCC IO template------ const int maxn=1000110; const double inf=999999999; #define lson (rt<<1),L,M #define rson (rt<<1|1),M+1,R #define M ((L+R)>>1) #define For(i,t,n) for(int i=(t);i<(n);i++) typedef long long LL; typedef double DB; #define bug printf("---\n"); #define mod 10007 struct edge { int from,to,cost; }es[maxn]; int V,E; int d[maxn]; void bell_man(int s) { fill(d,d+V+1,inf); d[s]=0; while(true) { bool update=false; for(int i=0;i<E;i++) { edge e=es[i]; if(d[e.from]!=inf&&d[e.to]>d[e.from]+e.cost) { d[e.to]=d[e.from]+e.cost; update=true; } } if(!update)break; } } int main() { //#ifndef ONLINE_JUDGE //freopen("in.txt","r",stdin); // #endif // ONLINE_JUDGE int n,m,i,j,k,t; while(read_(V,E)&&(V||E)) { E<<=1; for(i=0;i<E;i+=2) { int a,b,c; read__(a,b,c); es[i].from=a; es[i].to=b; es[i].cost=c; es[i+1].from=b; es[i+1].to=a; es[i+1].cost=c; } bell_man(1); writeln(d[V]); } return 0; }dijkstra 时间O(V*V)
//#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<string> #include<cstring> using namespace std; template<class T>inline T read(T&x) { char c; while((c=getchar())<=32)if(c==EOF)return -1; bool ok=false; if(c=='-')ok=true,c=getchar(); for(x=0; c>32; c=getchar()) x=x*10+c-'0'; if(ok)x=-x; return 1; } template<class T> inline T read_(T&x,T&y) { return read(x)!=-1&&read(y)!=-1; } template<class T> inline T read__(T&x,T&y,T&z) { return read(x)!=-1&&read(y)!=-1&&read(z)!=-1; } template<class T> inline void write(T x) { if(x<0)putchar('-'),x=-x; if(x<10)putchar(x+'0'); else write(x/10),putchar(x%10+'0'); } template<class T>inline void writeln(T x) { write(x); putchar('\n'); } //-------ZCC IO template------ const int maxn=101; const double inf=999999999; #define lson (rt<<1),L,M #define rson (rt<<1|1),M+1,R #define M ((L+R)>>1) #define For(i,t,n) for(int i=(t);i<(n);i++) typedef long long LL; typedef double DB; #define bug printf("---\n"); #define mod 10007 int G[maxn][maxn]; bool vis[maxn]; int d[maxn]; int V,E; void dij(int s) { For(i,0,V+1) { vis[i]=false; d[i]=inf; } d[s]=0; while(true) { int v=-1; For(u,1,V+1) if(!vis[u]&&(v==-1||d[v]>d[u])) v=u; if(v==-1)break; vis[v]=true; For(u,1,V+1) d[u]=min(d[u],d[v]+G[v][u]); } } int main() { //#ifndef ONLINE_JUDGE //freopen("in.txt","r",stdin); // #endif // ONLINE_JUDGE int n,m,i,j,k,t; while(read_(V,E)&&(V||E)) { For(i,1,V+1)For(j,1,V+1)G[i][j]=inf; for(i=0;i<E;i++) { int a,b,c; read__(a,b,c); G[a][b]=c; G[b][a]=c; } dij(1); writeln(d[V]); } return 0; }dijkstra使用堆优化版 时间O(E log V)
//#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<string> #include<queue> #include<vector> #include<map> #include<cstdlib> #include<set> #include<stack> #include<cstring> using namespace std; template<class T>inline T read(T&x) { char c; while((c=getchar())<=32)if(c==EOF)return -1; bool ok=false; if(c=='-')ok=true,c=getchar(); for(x=0; c>32; c=getchar()) x=x*10+c-'0'; if(ok)x=-x; return 1; } template<class T> inline T read_(T&x,T&y) { return read(x)!=-1&&read(y)!=-1; } template<class T> inline T read__(T&x,T&y,T&z) { return read(x)!=-1&&read(y)!=-1&&read(z)!=-1; } template<class T> inline void write(T x) { if(x<0)putchar('-'),x=-x; if(x<10)putchar(x+'0'); else write(x/10),putchar(x%10+'0'); } template<class T>inline void writeln(T x) { write(x); putchar('\n'); } //-------ZCC IO template------ const int maxn=101; const double inf=999999999; #define lson (rt<<1),L,M #define rson (rt<<1|1),M+1,R #define M ((L+R)>>1) #define For(i,t,n) for(int i=(t);i<(n);i++) typedef long long LL; typedef double DB; typedef pair<int,int> P; #define bug printf("---\n"); #define mod 10007 int V,E; struct edge { int to,cost; edge(int a,int b){to=a;cost=b;} }; vector<edge> G[maxn]; int d[maxn]; void dij_(int s) { priority_queue<P,vector<P>,greater<P> >q; fill(d,d+V+1,inf); d[s]=0; q.push(P(0,s)); while(!q.empty()) { P v=q.top(); q.pop(); if(d[v.second]<v.first)continue; for(int i=0;i<G[v.second].size();i++) { edge tmp=G[v.second][i]; if(d[tmp.to]>d[v.second]+tmp.cost) { d[tmp.to]=d[v.second]+tmp.cost; q.push(P(d[tmp.to],tmp.to)); } } } } int main() { //#ifndef ONLINE_JUDGE //freopen("in.txt","r",stdin); // #endif // ONLINE_JUDGE int n,m,i,j,k,t; while(read_(V,E)&&(V||E)) { For(i,0,E) { int a,b,c; read__(a,b,c); G[a].push_back(edge(b,c)); G[b].push_back(edge(a,c)); } dij_(1); writeln(d[V]); For(i,0,V+1)G[i].clear(); } return 0; }
标签:
原文地址:http://blog.csdn.net/u013167299/article/details/44994129