您的位置:首页 > 其它

HDU 1595 find the longest of the shortest

2016-09-26 16:22 274 查看

find the longest of the shortest

[align=left]Problem Description[/align]
Marica is very angry with Mirko because he found a new girlfriend and she seeks revenge.Since she doesn't live in the same city, she started preparing for the long journey.We know for every road how many minutes it takes to come from
one city to another.

Mirko overheard in the car that one of the roads is under repairs, and that it is blocked, but didn't konw exactly which road. It is possible to come from Marica's city to Mirko's no matter which road is closed.

Marica will travel only by non-blocked roads, and she will travel by shortest route. Mirko wants to know how long will it take for her to get to his city in the worst case, so that he could make sure that his girlfriend is out of town for long enough.Write
a program that helps Mirko in finding out what is the longest time in minutes it could take for Marica to come by shortest route by non-blocked roads to his city.
 

[align=left]Input[/align]
Each case there are two numbers in the first row, N and M, separated by a single space, the number of towns,and the number of roads between the towns. 1 ≤ N ≤ 1000, 1 ≤ M ≤ N*(N-1)/2. The cities are markedwith numbers from 1 to N,
Mirko is located in city 1, and Marica in city N.

In the next M lines are three numbers A, B and V, separated by commas. 1 ≤ A,B ≤ N, 1 ≤ V ≤ 1000.Those numbers mean that there is a two-way road between cities A and B, and that it is crossable in V minutes.
 

[align=left]Output[/align]
In the first line of the output file write the maximum time in minutes, it could take Marica to come to Mirko.
 

[align=left]Sample Input[/align]

5 6
1 2 4
1 3 3
2 3 1
2 4 4
2 5 7
4 5 1

6 7
1 2 1
2 3 4
3 4 4
4 6 4
1 5 5
2 5 2
5 6 5

5 7
1 2 8
1 4 10
2 3 9
2 4 10
2 5 1
3 4 7
3 5 10

 
[align=left]Sample Output[/align]

11
13
27

题意:输入还是普通的最短路输入,点和权值,其中有一段路径是坏的,但是不知道是那一条路,现在以最短路走,求出最坏的一种情况下的最短路,也就是所有可能性里最长的最短路。

先考虑正常情况下的最短路,断路一定在最短路上才可以影响最终的结果,所以要记住第一次的路径,然后逐个删去求最短路。

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define M 0xfffffff;
using namespace std;
int n,m;
int map[1002][1002];
int vis[1002];
int backto[10002],rlist[500002],rlist1[500002];
int e,minm;

void bfs()
{
int i;
minm=M;
e=0;
memset(vis,-1,sizeof(vis));
queue<int>q;
int front;
front=1;
vis[1]=0;
q.push(front);
memset(backto,0,sizeof(backto));
while(!q.empty())
{
front=q.front();
q.pop();
for(i=1; i<=n; i++)
{
if(map[front][i]>0&&(vis[front]+map[front][i]<vis[i]||vis[i]<0))
{
q.push(i);
vis[i]=vis[front]+map[front][i];
if(backto[i]==0)
e++;
backto[i]=front;
if(i==n&&vis[i]<minm)
{
minm=vis[i];
}
}
}
}
//printf("minm=%d \n",minm);
return ;
}

int g;//记录路径数组的长度
void goback()
{
int j,i,f=e;
int k=n;
for(j=e; k!=1; j--)
{
f--;
rlist[j]=backto[k];
k=backto[k];
}
g=0;
for(i=f+2; i<=e+1; i++)
{
rlist1[g]=rlist[i];
g++;
}
rlist1[g-1]=n;
}

int main()
{
int i,j,k,l;
while(~scanf("%d%d",&n,&m))
{
memset(map,-1,sizeof(map));
for(i=0; i<m; i++)
{
scanf("%d%d%d",&j,&k,&l);
if(map[j][k]>=0)
map[j][k]=min(map[j][k],l);
else
map[j][k]=l;
map[k][j]=map[j][k];
}
bfs();
goback();

int badr=0;
for(i=0; i<g; i++)
{
j=rlist1[i];
k=backto[j];
l=map[k][j];
map[k][j]=-1;map[j][k]=-1;
bfs();
if(minm==0xfffffff)
{
badr=-1;
break;
}
if(minm>badr)
{
badr=minm;
}
map[k][j]=l;map[j][k]=l;
}
printf("%d\n",badr);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM