find the longest of the shortest
2013-07-28 21:36
211 查看
#include<iostream> #include<vector> #include<string.h> #include<queue> using namespace std; #define INF 100000000 vector<int > g[1002]; int w[1002][1002],d[1002],vis[1002]; struct edge{int u;int v;}e[100000]; void spfa(int a,int n) { queue<int > q; memset(vis,0,sizeof(vis));//不在队列中 for(int i=2;i<=n;i++) d[i]=INF; d[1]=0; q.push(1); vis[1]=1; while(!q.empty()) { int a,b; a=q.front(); q.pop(); for(int i=0;i<g[a].size();i++) { b=g[a][i]; if(w[a][b]!=-1) { if(d[b]>d[a]+w[a][b]) { d[b]=d[a]+w[a][b]; if(vis[b]==0) q.push(b); } } } } } int main() { int i,j,n,m,a,b,c; while(scanf("%d%d",&n,&m)!=EOF) { memset(w,-1,sizeof(w)); for(int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); w[a][b]=c; w[b][a]=c; if(c>w[a][b]&&w[a][b]!=-1) continue; g[a].push_back(b); g[b].push_back(a); } spfa(1,n);// ok //printf("ans %d\n",d ); int u=n; int num=0; while(u!=1) { for(i=0;i<g[u].size ();i++) { int v=g[u][i]; if(d[v]+w[u][v]==d[u]) { edge temp; temp.u =u; temp.v=v; e[num++]=temp;u=v; break; } } } int maxx=0; for(i=0;i<num;i++) { int temp; temp=w[e[i].u ][e[i].v ]; w[e[i].u ][e[i].v]=w[e[i].v ][e[i].u]=-1; spfa(1,n); if(d >maxx) maxx=d ; w[e[i].u ][e[i].v]=w[e[i].v ][e[i].u]=temp; } printf("%d\n",maxx); // for(i=0;i<num;i++) //ok // printf("edge %d %d\n",e[i].u,e[i].v); for(i=1;i<=n;i++) g[i].clear(); } return 0; }
相关文章推荐
- hdu 1595 find the longest of the shortest(最短路spfa)
- hdu 1595 find the longest of the shortest ( spfa + 枚举 )
- HDU 1595 find the longest of the shortest
- hdu1595(spfa) find the longest of the shortest
- HDU1595 find the longest of the shortest(最短路)
- find the longest of the shortest HDU - 1595
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- HDU 1595 find the longest of the shortest (最短路)
- HDU1595 - find the longest of the shortest - 最短路变形+暴力
- hdu 1595 find the longest of the shortest (
- hdu 1595 find the longest of the shortest (Dijkstra+路径处理)
- 杭电 1595 find the longest of the shortest Dijstra
- HDU 1595 find the longest of the shortest
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- HDU 1595 find the longest of the shortest【次短路】
- hdu 1595 find the longest of the shortest【最短路+枚举】
- find the longest of the shortest HDU - 1595(最短路)
- HDU 1595 find the longest of the shortest
- 【HDU】1595 find the longest of the shortest 枚举+最短路
- hdu 1595 find the longest of the shortest