hdu 3986 Harry Potter and the Final Battle spfa变形
2015-08-14 19:45
423 查看
[code]#include<stdio.h> #include<string.h> #include<queue> #include<vector> using namespace std; const int N=1024; const int inf=0x7fffffff; struct Edge { int u,v,w,use,del; }; vector<Edge>edge; vector<int>G ; int n,m,dist ,inq ,mp ,path ,d ; void spfa() { int i,u,v; memset(inq,0,sizeof(inq)); memset(mp,0,sizeof(mp)); for(i=1;i<=n;i++) { dist[i]=inf; path[i]=1; } dist[1]=0; queue<int>q; q.push(1); inq[1]=1; while(!q.empty()) { u=q.front(); q.pop(); inq[u]=0; for(i=0;i<G[u].size();i++) { int t=G[u][i]; if(edge[t].del) continue; if(u==edge[t].u) v=edge[t].v; else v=edge[t].u; if(dist[v]>dist[u]+edge[t].w) { dist[v]=dist[u]+edge[t].w; mp[u][v]=t;path[v]=u; if(inq[v]==0) { q.push(v); inq[v]=1; } } } } } int main() { int _,i,j,u,v,w; Edge tp; scanf("%d",&_); while(_--) { edge.clear(); for(i=0; i<1024; i++) G[i].clear(); scanf("%d",&n); scanf("%d",&m); for(i=0; i<m; i++) { scanf("%d%d%d",&u,&v,&w); tp.u=u,tp.v=v,tp.w=w,tp.use=0,tp.del=0; edge.push_back(tp); G[u].push_back(i); G[v].push_back(i); } spfa(); if(dist ==inf) {printf("-1\n");continue;} int k1=n,k2=path ; while(1) { edge[mp[k2][k1]].use=1; if(k2==1) break; k1=k2; k2=path[k2]; } int ans=-1; for(i=0; i<m; i++) { if(edge[i].use) { edge[i].del=1; spfa(); if(dist ==inf) { ans=-1;break; } ans=max(ans,dist ); edge[i].del=0; } } printf("%d\n",ans); } return 0; }
相关文章推荐
- 黑马程序员-多态的讲解
- UVa 439-Knight Moves
- 求两个串的最大公共子串
- 使用reserve要再次避免不必要的分配
- hdu 3986 Harry Potter and the Final Battle spfa变形
- ListView/gridView代码优化viewHolder精解--converView使用
- Nginx内存管理
- C# axWindowsMediaPlayer制作播放器
- POJ 2367 Genealogical tree(拓扑排序)
- 线程池(二)
- 摄像机标定方法(二)----Faugeras的摄像机标定方法
- poj 2367 Genealogical tree【拓扑排序输出可行解】
- 排版紧凑情况下IOS 浏览器的文字部分选中问题
- C语言-母牛和兔子的问题
- PPT自动载入图片并矩阵分布
- android 中的Uri
- python:元组和小括号的误导
- hdu 1203
- Android TextView中文字通过SpannableString来设置超链接、颜色、字体等属性
- 求两圆相交面积模板