hdu 1595 find the longest of the shortest
2017-08-02 14:17
267 查看
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.
InputEach 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.OutputIn the first line of the output file write the maximum time in minutes, it could take Marica to come to Mirko.Sample Input
Sample Output
这道题的题意是说,Marica在1号城市,她发现在n号城市的Mirko有了新女友,很生气,决定去Mirko的城市找他的新女友。从1号城市到n号城市有很多条路径,每条路都有走这条路所花费的时间,Marica想尽可能早的到达n城市。但是呢,在去1-n的路上,这里有且仅有一条路是在修,不能走。Mirko当然希望绕过这条路后Mirko到达他所在的城市用的时间越多越好,而Marica只想尽快到,所以即使绕过这条路,她也要走其他花费时间最少的路。也就是说,Marica一定会走最短的路。问,现在假如让Mirko选择一条路被破坏掉,那么Marica能到达n城市最坏情况下的花费时间是多少。
其实意思就是说,Mirko每选择一条路被破坏掉,Marica相应的就会走在没有这条路的情况下,最短的路。那么就问,Mirko要怎样选择才能使Marica走的最短路在所有的最短路中是最长的,输出Marica走的最短路路程是多少。
如果要想Marica走最长的最短路,那么一定要选择正常情况下,从1-n的最短路所经过的街道,选择这些街道将它破坏。因为如果不选最短路上的街道,那么Marica就可以走正常情况下的最短路,那么用的时间就会很少。所以一定要破坏掉最短路上的街道。
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#define N 1010
#define INF 0x3f3f3f3f
using namespace std;
int e
,b
,dis
,path
,Min_path
,k,Min,m,n;
void dijkstra(int flag)
{
for(int i=1; i<=n; i++)
{
dis[i]=e[1][i];
b[i]=0;
}
b[1]=1,dis[1]=0;
int u,v;
for(int i=1; i<n; i++)
{
Min=INF;
for(int j=1; j<=n; j++)
{
if(b[j]==0&&dis[j]<Min)
{
Min=dis[j];
u=j;
}
}
b[u]=1;
for(v=1; v<=n; v++)
{
if(dis[v]>dis[u]+e[u][v]&&b[v]==0&&e[u][v]<INF)
{
dis[v]=dis[u]+e[u][v];
if(flag)//只有在第一次求最短路的时候才能改变path数组,不然每次跑dij都更新的话,path里存的最短路就会不一样
path[v]=u;
}
}
}
if(flag)
{
int j=n;
k=0;
while(path[j]!=-1)//求正常情况下的最短路,存储到数组里
{
Min_path[k++]=j;
j=path[j];
}
Min_path[k++]=j;
Min_path[k++]=1;
}
}
int main()
{
int t1,t2,t3;
while(cin>>n>>m)
{
memset(e,INF,sizeof(e));
for(int i=1; i<=m; i++)
{
scanf("%d%d%d",&t1,&t2,&t3);
e[t1][t2]=e[t2][t1]=t3;
}
memset(path,-1,sizeof(path));
dijkstra(1);
int ans=0,num;
for(int i=0; i<k-1; i++)//每次选择最短路中的一条边进行破坏
{
int tx=Min_path[i];
int ty=Min_path[i+1];
num=e[tx][ty];
e[tx][ty]=e[ty][tx]=INF;
dijkstra(0);
ans=max(ans,dis
);//求最短路中的最大值
e[tx][ty]=e[ty][tx]=num;//求完最短路都要把破坏的边再变回来
}
printf("%d\n",ans);
}
return 0;
}
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.
InputEach 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.OutputIn 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
这道题的题意是说,Marica在1号城市,她发现在n号城市的Mirko有了新女友,很生气,决定去Mirko的城市找他的新女友。从1号城市到n号城市有很多条路径,每条路都有走这条路所花费的时间,Marica想尽可能早的到达n城市。但是呢,在去1-n的路上,这里有且仅有一条路是在修,不能走。Mirko当然希望绕过这条路后Mirko到达他所在的城市用的时间越多越好,而Marica只想尽快到,所以即使绕过这条路,她也要走其他花费时间最少的路。也就是说,Marica一定会走最短的路。问,现在假如让Mirko选择一条路被破坏掉,那么Marica能到达n城市最坏情况下的花费时间是多少。
其实意思就是说,Mirko每选择一条路被破坏掉,Marica相应的就会走在没有这条路的情况下,最短的路。那么就问,Mirko要怎样选择才能使Marica走的最短路在所有的最短路中是最长的,输出Marica走的最短路路程是多少。
如果要想Marica走最长的最短路,那么一定要选择正常情况下,从1-n的最短路所经过的街道,选择这些街道将它破坏。因为如果不选最短路上的街道,那么Marica就可以走正常情况下的最短路,那么用的时间就会很少。所以一定要破坏掉最短路上的街道。
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#define N 1010
#define INF 0x3f3f3f3f
using namespace std;
int e
,b
,dis
,path
,Min_path
,k,Min,m,n;
void dijkstra(int flag)
{
for(int i=1; i<=n; i++)
{
dis[i]=e[1][i];
b[i]=0;
}
b[1]=1,dis[1]=0;
int u,v;
for(int i=1; i<n; i++)
{
Min=INF;
for(int j=1; j<=n; j++)
{
if(b[j]==0&&dis[j]<Min)
{
Min=dis[j];
u=j;
}
}
b[u]=1;
for(v=1; v<=n; v++)
{
if(dis[v]>dis[u]+e[u][v]&&b[v]==0&&e[u][v]<INF)
{
dis[v]=dis[u]+e[u][v];
if(flag)//只有在第一次求最短路的时候才能改变path数组,不然每次跑dij都更新的话,path里存的最短路就会不一样
path[v]=u;
}
}
}
if(flag)
{
int j=n;
k=0;
while(path[j]!=-1)//求正常情况下的最短路,存储到数组里
{
Min_path[k++]=j;
j=path[j];
}
Min_path[k++]=j;
Min_path[k++]=1;
}
}
int main()
{
int t1,t2,t3;
while(cin>>n>>m)
{
memset(e,INF,sizeof(e));
for(int i=1; i<=m; i++)
{
scanf("%d%d%d",&t1,&t2,&t3);
e[t1][t2]=e[t2][t1]=t3;
}
memset(path,-1,sizeof(path));
dijkstra(1);
int ans=0,num;
for(int i=0; i<k-1; i++)//每次选择最短路中的一条边进行破坏
{
int tx=Min_path[i];
int ty=Min_path[i+1];
num=e[tx][ty];
e[tx][ty]=e[ty][tx]=INF;
dijkstra(0);
ans=max(ans,dis
);//求最短路中的最大值
e[tx][ty]=e[ty][tx]=num;//求完最短路都要把破坏的边再变回来
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- 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
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- 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(spfa)(存储并枚举最短路径的边)
- find the longest of the shortest HDU - 1595(最短路)
- 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
- hdu 1595 find the longest of the shortest
- hdu 1595 find the longest of the shortest(dijstra + 枚举)
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- hdu 1595 find the longest of the shortest
- 【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
- HDU 1595 find the longest of the shortest 删除一条边的最长最短路