Harry Potter and the Final Battle
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 255 Accepted Submission(s): 89
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
Author
tender@WHU
Source
2011 Multi-University Training Contest 15 - Host by WHU
Recommend
lcy
题目: http://acm.hdu.edu.cn/showproblem.php?pid=3986
PS:首先鄙视下自己比赛时直接想复杂了。。。懒得写堆维护用spfa,再次。。。
分析:这题要求去掉一条边,使得最短路最长,首先确定去掉不是最短路上的边就是无用功了,所以,那条边必须是最短路上的边,于是先做一次最短路,求出最短路上的边,然后枚举去掉哪条边,再求最短路,使得最短路最长即答案,注意重边的情况,答案赋初值为第一次最短路的的答案,因此wa了一次,,,,囧
代码:
#include<cstdio>
using namespace std;
const int mm=111111;
const int mn=1111;
const int oo=1000000000;
int ver[mm],next[mm];
int head[mn],dis[mn],q[mm],p[mn],d[mn][mn][2];
bool vis[mn];
int n,edge;
inline int min(int a,int b)
{
return a<b?a:b;
}
inline int max(int a,int b)
{
return a>b?a:b;
}
inline void prepare(int n)
{
while(n)vis[n]=0,head[n--]=-1;
edge=0;
}
inline void addedge(int u,int v,int c)
{
if(c<d[u][v][0])
{
if(d[u][v][0]==oo)
{
d[u][v][0]=d[v][u][0]=c;
ver[edge]=v,next[edge]=head[u],head[u]=edge++;
ver[edge]=u,next[edge]=head[v],head[v]=edge++;
}
else d[v][u][1]=d[u][v][1]=d[u][v][0],d[u][v][0]=d[v][u][0]=c;
}
else d[v][u][1]=d[u][v][1]=min(d[u][v][1],c);
}
int spfa(bool save)
{
int i,u,v,l,r=0,tmp;
for(i=1;i<=n;++i)dis[i]=oo;
dis[q[r++]=1]=0;
if(save)p[1]=p[n]=-1;
for(l=0;l!=r;(++l>=mm)?l=0:1)
for(i=head[u=q[l]],vis[u]=0;i>=0;i=next[i])
if(dis[v=ver[i]]>(tmp=dis[u]+d[u][v][0]))
{
dis[v]=tmp;
if(save)p[v]=u;
if(vis[v])continue;
vis[q[r++]=v]=1;
if(r>=mm)r=0;
}
return dis[n];
}
int main()
{
int i,j,k,m,t,ans;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;++i)
for(j=1;j<=n;++j)d[i][j][0]=d[i][j][1]=oo;
prepare(n);
while(m--)scanf("%d%d%d",&i,&j,&k),addedge(i,j,k);
ans=spfa(1);
for(i=n,j=p[n];i!=1&&j>0;i=j,j=p[j])
{
k=d[i][j][0];
d[i][j][0]=d[j][i][0]=d[i][j][1];
ans=max(ans,spfa(0));
d[i][j][0]=d[j][i][0]=k;
if(ans>=oo)break;
}
if(ans>=oo)printf("-1\n");
else printf("%d\n",ans);
}
return 0;
}