您的位置:首页 > 其它

hdu 3986(最短路变形好题)

2016-05-20 15:21 351 查看

Harry Potter and the Final Battle

Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3239 Accepted Submission(s): 906


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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”.

[align=left]Sample Input[/align]

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

[align=left]Sample Output[/align]

15
-1
2
hdu1595的升级版

1595不需要考虑重边,而这道题要考虑,所以要用链式前向星保存,比较好的解法是spfa,我们第一次求最短路的时候用一个数组保存下这条边,然后后面枚举最短路上面的边的时候需要用到这个数组,通过这个题又学到一个好方法,如何在遍历图保的时候存访问过的边。

#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <queue>
using namespace std;
const int N = 1005;
const int M = 100005;
const int INF = 999999999;
struct Edge
{
int u,v,w,next;
} edge[M];
int head
;
int pre
;
int n,m;
bool vis
;
int low
;
int _edge[M]; ///标记下哪条边用过了
void addEdge(int u,int v,int w,int &k)
{
edge[k].v = v,edge[k].w = w;
edge[k].next=head[u],head[u]=k++;
}
int spfa(int s,int flag)
{
queue<int> q;
for(int i=1; i<=n; i++)
{
if(flag)
{
pre[i] = s;
}
low[i] = INF;
vis[i] = false;
}
low[s] = 0;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int k = head[u]; k!=-1; k=edge[k].next)
{
int v = edge[k].v,w=edge[k].w;
if(low[v]>low[u]+w)
{
low[v] = low[u]+w;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
if(flag)
{
pre[v] = u;
_edge[v] = k; ///标记最短路上哪条边被用了
}
}
}
}
if(low
>=INF) return -1;
return low
;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--)
{
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
int tot = 0;
for(int i=0; i<m; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addEdge(u,v,w,tot);
addEdge(v,u,w,tot);
}
int res = spfa(1,1);
if(res==-1)
{
printf("-1\n");
continue;
}
int temp = n;
int Max= -1;
while(temp!=1)
{
int e = _edge[temp];
int k = edge[e].w;
edge[e].w=INF;
int res = spfa(1,0);
if(res==-1)
{
Max = -1;
break;
}
Max = max(Max,res);
edge[e].w=k;
temp = pre[temp];
}
printf("%d\n",Max);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: