Harry Potter and the Final Battle
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3278 Accepted Submission(s): 915
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
Author
tender@WHU
题目大意:有t组数据,每组数据有n个点m条边。哈利波特想从1号到达n号,然而住在n号的人不详让哈利波特过来,所以他使用魔法摧毁了一条边,问,巫师最大能让哈利波特最慢过来的时间耗费。如果摧毁了一条边之后,哈利波特过不来了,输出-1.
思路:
1、首先我们知道,如果删除的边是非从节点1所求得的最短路径上的边,那么这条边删除与否,根本不会影响哈利波特到达n点的时间耗费,所以我们要删除的边,一定是从节点1求得的最短路径上边的边。
2、对于我们要具体删除哪一条边才能使得结果正确,我们是不从得知的,所以我们枚举这从1到n的最短路径上边的边,枚举每一段,删除,然后再求一遍最短路,记录最大值。
3、题意中说有重边,而且存在相同权值重边,所以我们建立邻接表,记录其边的编号,然后在记录路径的时候,记录其编号即可。
AC代码:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
int head[10000];
struct Edage
{
int flag;
int next;
int w;
int to;
}e[500000];
int dis[10000];
int del[1000000][2];
int path[100000][2];
int vis[10000];
int cont,n,m;
void add(int from,int to,int w)
{
e[cont].to=to;
e[cont].flag=cont;
e[cont].w=w;
e[cont].next=head[from];
head[from]=cont++;
}
int SPFA(int ss,int judge)
{
for(int i=1;i<=n;i++)
{
dis[i]=0x3f3f3f3f;
}
memset(vis,0,sizeof(vis));
queue<int >s;
s.push(ss);
vis[ss]=1;
dis[ss]=0;
while(!s.empty())
{
int u=s.front();
s.pop();vis[u]=0;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
int w=e[i].w;
if(judge!=-1&&e[i].flag==del[judge][0]||e[i].flag==del[judge][1])continue;
if(dis[v]>dis[u]+w)
{
path[v][0]=u;
path[v][1]=e[i].flag;
dis[v]=dis[u]+w;
if(vis[v]==0)
{
vis[v]=1;
s.push(v);
}
}
}
}
return dis[n];
}
void Slove()
{
memset(path,-1,sizeof(path));
int output=SPFA(1,-1);
int now=n;
//printf("%d\n",n);
int tt=0;
while(1)
{
//printf("%d %d\n",path[now][0],path[now][1]);
del[tt][0]=path[now][1];
del[tt++][1]=path[now][1]%2==0?path[now][1]+1:path[now][1]-1;
now=path[now][0];
if(path[now][0]==-1)break;
}
for(int i=0;i<tt;i++)
{
output=max(SPFA(1,i),output);
}
if(output>=0x3f3f3f3f)printf("-1\n");
else printf("%d\n",output);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
cont=0;
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
add(x,y,w);
add(y,x,w);
}
Slove();
}
}