HDU 3986 Harry Potter and the Final Battle(Dijkstra)
来源:互联网 发布:淘宝海外直邮要身份证 编辑:程序博客网 时间:2024/05/16 11:54
HDU 3986 Harry Potter and the Final Battle(Dijkstra)
http://acm.hdu.edu.cn/showproblem.php?pid=3986
题意:
给你一个无向图,现在在删除图中任意一条边的情况下,要你求从1号点到N号点的最短距离的所有可能值中的最大值.
分析:
明显删除的这条边一定要在1到N的最短路径上才是对结果有影响的.所以我们只需要标记所有存在最短路径上的边,然后一次枚举不走该边的情况下计算最短路径值即可.
AC代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#define INF 1e9using namespace std;const int maxn =1000+10;const int maxm=50000*2+10;struct Edge{ int from,to,dist; Edge(){} Edge(int f,int t,int d):from(f),to(t),dist(d){}};struct HeapNode{ int d,u; HeapNode(int d,int u):d(d),u(u){} bool operator<(const HeapNode &rhs)const { return d>rhs.d; }};struct Dijkstra{ int n,m; Edge edges[maxm]; int head[maxn],next[maxm]; int d[maxn]; bool done[maxn]; int p[maxn]; void init(int n) { this->n=n; m=0; memset(head,-1,sizeof(head)); } void AddEdge(int from,int to,int dist) { edges[m]=Edge(from,to,dist); next[m]=head[from]; head[from] = m++; } int dijkstra(int edge_id)//不走edge_id这条边 { priority_queue<HeapNode> Q; for(int i=0;i<n;i++) d[i]= i==0?0:INF; memset(done,0,sizeof(done)); Q.push(HeapNode(d[0],0)); while(!Q.empty()) { HeapNode x=Q.top(); Q.pop(); int u=x.u; if(done[u]) continue; done[u]=true; for(int i=head[u];i!=-1;i=next[i])if(i != edge_id) { Edge &e=edges[i]; if(d[e.to] > d[u]+e.dist) { d[e.to] = d[u]+e.dist; p[e.to]=i;//标记最短路的最后一条边 Q.push(HeapNode(d[e.to],e.to)); } } } return d[n-1]; }}DJ;int main(){ int T; scanf("%d",&T); while(T--) { int n,m; scanf("%d%d",&n,&m); DJ.init(n); while(m--) { int u,v,d; scanf("%d%d%d",&u,&v,&d); u--,v--; DJ.AddEdge(u,v,d); DJ.AddEdge(v,u,d); } if(DJ.dijkstra(-1)==INF) { printf("-1\n"); continue; } vector<int> E;//保存最短路上边的序号 int ed=n-1; while(ed!=0) { int edge_id = DJ.p[ed]; E.push_back(edge_id); ed= DJ.edges[edge_id].from; } int ans = -1; for(int i=0;i<E.size();i++) ans = max(ans,DJ.dijkstra(E[i])); if(ans==INF) printf("-1\n"); else printf("%d\n",ans); } return 0;}
0 0
- 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
- 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 spfa变形
- HDU 3986 Harry Potter and the Final Battle 最短路
- HDU-3986-Harry Potter and the Final Battle
- hdu3986 Harry Potter and the Final Battle
- hdoj 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(最短路+枚举删边)
- Oracle 权限
- java replaceAll() 方法要用 4 个反斜杠,表示一个反斜杠
- 常见渗透端口
- SQL优化规则
- RubyOnRails开发知识链接汇总
- HDU 3986 Harry Potter and the Final Battle(Dijkstra)
- SGU 537 Divisibility 枚举
- POJ3259
- Java写人机猜拳游戏(可扩展其他游戏或其他参与者)
- 福建省队集训被虐记——DAY4
- 基于Go的D-Bus框架(一)
- FZU 2146 Easy Game
- Openfire 的安装和配置
- POJ3080