hdu 1595 find the longest of the shortest (
2016-09-26 23:09
387 查看
Problem Description
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.
Input
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.
Output
In the first line of the output file write the maximum time in minutes, it could take Marica to come to Mirko.
Sample Input
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
Sample Output
11
13
27
题意:城市内有n条路,其中有某条路在修,因为这条路有很多情况,问各种情况下的最短路中最长的是哪条;
思路:假设图中某条路径被堵死,它的最坏情况下的最短路径是多少?基本算法就是先求出最短路径,然后假设最短路径中的某一条边被堵死,再求最短路,取这些最短路的最大值即可。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct node{
int l,t;
}d[1005],map[1005][1005];
const int maxn=0xfffffff;
int vis[1005],mag[5000000];
int n,m,maxx;
void dij()
{
for(int i=1;i<n;i++)
{
int minn=maxn,k;
for(int j=1;j<=n;j++)
if(!vis[j]&&d[j].l<minn)
{
minn=d[j].l;
k=j;
}
vis[k]=1;
if(k==n)
break;
for(int j=1;j<=n;j++)
if(!vis[j]&&map[k][j].l!=-1&&map[k][j].l+d[k].l<d[j].l)
{
d[j].l=map[k][j].l+d[k].l;
d[j].t=k;
}
}
}
void query()
{
for(int i=1;i<=m;i++)
{
if(mag[i])
{
memset(vis,0,sizeof(vis));
memset(d,0,sizeof(d));
int temp,x,y,flag=0;
for(int j=1;j<=n;j++)
{
for(int k=j+1;k<=n;k++)
if(map[j][k].t==i)
{
x=k;y=j;
temp=map[j][k].l;
map[j][k].l=-1;
map[k][j].l=-1;
flag=1;
break;
}
if(flag)
break;
}
for(int j=1;j<=n;j++)
{
if(map[1][j].l!=-1)
{
d[j].l=map[1][j].l;
d[j].t=1;
}
else
d[j].l=maxn;
}
dij();
maxx=max(maxx,d
.l);
map[x][y].l=temp;
map[y][x].l=temp;
}
else
continue;
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(map,-1,sizeof(map));
memset(vis,0,sizeof(vis));
memset(mag,0,sizeof(mag));
memset(d,0,sizeof(d));
for(int i=1;i<=m;i++)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
map[a][b].l=w;
map[b][a].l=w;
map[a][b].t=map[b][a].t=i;
}
for(int i=1;i<=n;i++)
{
if(map[1][i].l!=-1)
{
d[i].l=map[1][i].l;
d[i].t=1;
}
else
d[i].l=maxn;
}
vis[1]=1;
dij();
maxx=d
.l;
int k=n;
while(k!=1)
{
mag[map[k][d[k].t].t]=1;
k=d[k].t;
}
query();
printf("%d\n",maxx);
}
}
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.
Input
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.
Output
In the first line of the output file write the maximum time in minutes, it could take Marica to come to Mirko.
Sample Input
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
Sample Output
11
13
27
题意:城市内有n条路,其中有某条路在修,因为这条路有很多情况,问各种情况下的最短路中最长的是哪条;
思路:假设图中某条路径被堵死,它的最坏情况下的最短路径是多少?基本算法就是先求出最短路径,然后假设最短路径中的某一条边被堵死,再求最短路,取这些最短路的最大值即可。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct node{
int l,t;
}d[1005],map[1005][1005];
const int maxn=0xfffffff;
int vis[1005],mag[5000000];
int n,m,maxx;
void dij()
{
for(int i=1;i<n;i++)
{
int minn=maxn,k;
for(int j=1;j<=n;j++)
if(!vis[j]&&d[j].l<minn)
{
minn=d[j].l;
k=j;
}
vis[k]=1;
if(k==n)
break;
for(int j=1;j<=n;j++)
if(!vis[j]&&map[k][j].l!=-1&&map[k][j].l+d[k].l<d[j].l)
{
d[j].l=map[k][j].l+d[k].l;
d[j].t=k;
}
}
}
void query()
{
for(int i=1;i<=m;i++)
{
if(mag[i])
{
memset(vis,0,sizeof(vis));
memset(d,0,sizeof(d));
int temp,x,y,flag=0;
for(int j=1;j<=n;j++)
{
for(int k=j+1;k<=n;k++)
if(map[j][k].t==i)
{
x=k;y=j;
temp=map[j][k].l;
map[j][k].l=-1;
map[k][j].l=-1;
flag=1;
break;
}
if(flag)
break;
}
for(int j=1;j<=n;j++)
{
if(map[1][j].l!=-1)
{
d[j].l=map[1][j].l;
d[j].t=1;
}
else
d[j].l=maxn;
}
dij();
maxx=max(maxx,d
.l);
map[x][y].l=temp;
map[y][x].l=temp;
}
else
continue;
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(map,-1,sizeof(map));
memset(vis,0,sizeof(vis));
memset(mag,0,sizeof(mag));
memset(d,0,sizeof(d));
for(int i=1;i<=m;i++)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
map[a][b].l=w;
map[b][a].l=w;
map[a][b].t=map[b][a].t=i;
}
for(int i=1;i<=n;i++)
{
if(map[1][i].l!=-1)
{
d[i].l=map[1][i].l;
d[i].t=1;
}
else
d[i].l=maxn;
}
vis[1]=1;
dij();
maxx=d
.l;
int k=n;
while(k!=1)
{
mag[map[k][d[k].t].t]=1;
k=d[k].t;
}
query();
printf("%d\n",maxx);
}
}
相关文章推荐
- HDU 1595 find the longest of the shortest
- hdu 1595 find the longest of the shortest ( spfa + 枚举 )
- HDU - 1595 find the longest of the shortest(最短路Dijkstra+枚举删边)
- hdu 1595 find the longest of the shortest(dijstra + 枚举)
- HDU 1595 find the longest of the shortest
- HDU 1595 find the longest of the shortest (最短路+记录路径+枚举删边)
- HDU 1595 find the longest of the shortest 删掉任意一条边的最长最短路
- HDU 1595 find the longest of the shortest【次短路】
- HDU 1595 find the longest of the shortest
- HDU 1595 find the longest of the shortest
- HDU 1595 find the longest of the shortest(枚举,最短路)
- hdu 1595 find the longest of the shortest (枚举+最短路)
- HDU 1595 find the longest of the shortest
- HDU 1595 find the longest of the shortest
- HDU 1595——find the longest of the shortest
- HDU 1595 find the longest of the shortest 删除一条边的最长最短路
- HDU 1595 find the longest of the shortest
- HDU 1595 find the longest of the shortest(Dijkstra)
- hdu 1595 find the longest of the shortest (spfa)
- 【HDU】1595 find the longest of the shortest 枚举+最短路