hdu 3986 Harry Potter and the Final Battle spfa变形
来源:互联网 发布:linux怎么禁ping 编辑:程序博客网 时间:2024/06/05 20:41
#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[N];int n,m,dist[N],inq[N],mp[N][N],path[N],d[N];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[n]==inf) {printf("-1\n");continue;} int k1=n,k2=path[n]; 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[n]==inf) { ans=-1;break; } ans=max(ans,dist[n]); edge[i].del=0; } } printf("%d\n",ans); } return 0;}
0 0
- hdu 3986 Harry Potter and the Final Battle spfa变形
- hdu 3986 Harry Potter and the Final Battle(最短路变形)
- HDU - 3986 Harry Potter and the Final Battle(最短路SPFA+枚举删边)
- hdu 3986 Harry Potter and the Final Battle 最短路spfa+枚举
- hdu 3986 Harry Potter and the Final Battle【最短路SPFA】
- hdu 3986 Harry Potter and the Final Battle
- HDU 3986 Harry Potter and the Final Battle
- hdu 3986 Harry Potter and the Final Battle
- hdu 3986 Harry Potter and the Final Battle
- HDU 3986 Harry Potter and the Final Battle
- hdu 3986 Harry Potter and the Final Battle
- HDU 3986 Harry Potter and the Final Battle(Dijkstra)
- 【HDU】3986 Harry Potter and the Final Battle 最短路
- hdu 3986 Harry Potter and the Final Battle
- HDU 3986 Harry Potter and the Final Battle
- HDU 3986 Harry Potter and the Final Battle 最短路
- HDU-3986-Harry Potter and the Final Battle
- hdu3986 Harry Potter and the Final Battle
- Android ListView/gridView代码优化ViewHolder,converView使用
- RedHat Enterprise Linux5关于中文显示乱码问题解决
- HD1285 确定比赛名次 【拓扑排序】
- Hadoop之SequenceFile
- 黑马程序员-多态的讲解
- hdu 3986 Harry Potter and the Final Battle spfa变形
- UVa 439-Knight Moves
- Oracle开发中的正则表达式
- PLSQL操作Oracle创建用户和表
- visualsvn在win8下本地内网使用架设小记
- OC - SortArray
- leetcode 138 —— Copy List with Random Pointer
- A - Dungeon Master
- 小白学开发(iOS)OC_ 字符串的比较判断(2015-08-12)