hdu 3986 Harry Potter and the Final Battle(最短路变形)
2016-05-30 10:28
561 查看
Harry Potter and the Final Battle
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3267 Accepted Submission(s): 913
Problem Description
The final battle is coming. Now Harry Potter is located at city 1, and Voldemort is located at city n. To make the world peace as soon as possible, Of course, Harry Potter will choose the shortest road between city 1 and city n. But unfortunately, Voldemort
is so powerful that he can choose to destroy any one of the existing roads as he wish, but he can only destroy one. Now given the roads between cities, you are to give the shortest time that Harry Potter can reach city n and begin the battle in the worst case.
Input
First line, case number t (t<=20).
Then for each case: an integer n (2<=n<=1000) means the number of city in the magical world, the cities are numbered from 1 to n. Then an integer m means the roads in the magical world, m (0< m <=50000). Following m lines, each line with three integer u, v,
w (u != v,1 <=u, v<=n, 1<=w <1000), separated by a single space. It means there is a bidirectional road between u and v with the cost of time w. There may be multiple roads between two cities.
Output
Each case per line: the shortest time to reach city n in the worst case. If it is impossible to reach city n in the worst case, output “-1”.
Sample Input
3
4
4
1 2 5
2 4 10
1 3 3
3 4 8
3
2
1 2 5
2 3 10
2
2
1 2 1
1 2 2
Sample Output
15
-1
2
题意:求1~n的最短路,在仅有的m条路里,会有一条会被毁灭,但是不知道是哪一条,所以让你求出最差的情况下的最短路是多少。
思路:如果毁灭的路不在原本的最短路上,那么毁灭了也不受影响,所以我们只对最短路上的路径进行处理。此题难点在于重边.即原本最短路径上有1-2且长度为5的路径,但是该路径有多条的话,毁灭了这条路径最短路依然不变,所以我们不能单纯让这两点之间的路消失。这里用二维数组和数量数组去标记的话会TLE,所以这里就需要用到链式前向星了。 我们只需要把路径权值设为MAX,那么理论上这条路径也不存在了,但是其他相同的边仍然存在。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 1010
#define M 50050
#define INF 1<<28
struct Edge
{
int v,next,w;
} edge[M*2];
int n,m,cnt;
int head
;
int vis
,d
;
struct Node
{
int pre,id;
} used
;
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)
{
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void djstl(int s,int t)
{
for(int i=1; i<=n; i++)
{
d[i]=INF;
if(!t) used[i].pre=s;
}
for(int u=head[s]; u!=-1; u=edge[u].next)
{
if(edge[u].w<d[edge[u].v])
{
d[edge[u].v]=edge[u].w;
if(!t) used[edge[u].v].id=u;
};
}
memset(vis,0,sizeof(vis));
vis[s]=1;
for(int i=1; i<=n; i++)
{
int maxn=INF;
int v=-1;
for(int j=1; j<=n; j++)
if(!vis[j]&&maxn>d[j])
{
maxn=d[j];
v=j;
}
if(v==-1) continue;
vis[v]=1;
for(int u=head[v]; u!=-1; u=edge[u].next)
{
int e=edge[u].v;
if(!vis[e]&&d[v]+edge[u].w<d[e])
{
d[e]=d[v]+edge[u].w;
if(!t) used[e].pre=v,used[e].id=u;
}
}
}
}
int main()
{
int s,t,w;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
init();
for(int i=0; i<m; i++)
{
scanf("%d %d %d",&s,&t,&w);
addedge(s,t,w);
addedge(t,s,w);
}
djstl(1,0);
int maxn=d
;
for(int i=n; i!=1; i=used[i].pre)
{
int val=edge[used[i].id].w;
edge[used[i].id].w=INF;
djstl(1,1);
edge[used[i].id].w=val;
maxn=max(maxn,d
);
}
if(maxn>=INF) printf("-1\n");
else printf("%d\n",maxn);
}
return 0;
}
相关文章推荐
- PHP 模糊搜索
- ajax库类以及使用方法
- 文件编码及UTF-8、BOM、0XFEFF相关问题
- iOS之 FBMemoryProfiler FB的循环引用检测工具
- hdu 2841(容斥原理+状态压缩)
- fragement生命周期
- liunx 编程常用命令(expand,grep ,find )
- oracle-序列 ora-02287 此处不允许序号
- Vim杂记:Sublime的配色方案
- tomcat启动异常:IOException while loading persisted sessions: java.io.EOFExceptio 4000 n
- MVC+EF+Oracle运行时 “System.ArgumentException”异常
- JavaScript学习--Item18 JScript的Bug与内存管理
- if()return;的一点小问题
- 数据库查询
- Git速成
- python 高级特性
- iOS 多线程篇1—多线程简单介绍
- git init 和 git init –bare
- 使用JQuery 加载页面时调用JS的实现方法
- oracle数据库解析json格式