HDu 1595最短回路アルゴリズムdijkstra
9710 ワード
find the longest of the shortest
Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2094 Accepted Submission(s): 739
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,mを2つあげて、n都市mの道があることを示して、次はm行で、各行の3つの数字は起点の終点とかかる時間を表して、注意して、経路は双方向です.それから最短の経路を求めますが、このmの経路は結局1つの道が歩けないので、最悪の場合の最短の経路を要求します.
考え方:最悪の場合の最短経路を考えると、歩けない道は必ず最短経路の上にあり、すべて最短経路を見つけて、それぞれ最短経路のいずれかを取り除いてみて、それから短経路を求めて、最後に最小経路の最大値を取るのが最悪の場合の最短経路です.本コードではdijkstraアルゴリズムを用いて最短ルートを求める.
acコード:
Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2094 Accepted Submission(s): 739
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,mを2つあげて、n都市mの道があることを示して、次はm行で、各行の3つの数字は起点の終点とかかる時間を表して、注意して、経路は双方向です.それから最短の経路を求めますが、このmの経路は結局1つの道が歩けないので、最悪の場合の最短の経路を要求します.
考え方:最悪の場合の最短経路を考えると、歩けない道は必ず最短経路の上にあり、すべて最短経路を見つけて、それぞれ最短経路のいずれかを取り除いてみて、それから短経路を求めて、最後に最小経路の最大値を取るのが最悪の場合の最短経路です.本コードではdijkstraアルゴリズムを用いて最短ルートを求める.
acコード:
#include<iostream>
#include<cstdio>
#include<climits>
#include<cstring>
using namespace std;
int road[1005][1005];
int n,m;
int fa[1005];
int dist[1005];
int vis[1005];
int sum;
int Min(int a,int b)
{
return a<b?a:b;
}
int Max(int a,int b)
{
return a>b?a:b;
}
int dijk(int start,int last)
{
for(int i=1;i<=n;i++)
{
dist[i]=INT_MAX;
}
dist[start]=0;
memset(vis,0,sizeof(vis));
for(int j=0;j<n;j++)
{
int m=INT_MAX,x;
for(int i=1;i<=n;i++)
{
if(!vis[i]&&dist[i]<m)
{
m=dist[i];
x=i;
}
}
vis[x]=1;
for(int i=1;i<=n;i++)
{
if(road[x][i]!=-1)
{
dist[i]=Min(dist[i],dist[x]+road[x][i]);
}
}
}
return dist[last];
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(road,-1,sizeof(road));
for(int i=0;i<m;i++)
{
int start,last,temp;
scanf("%d%d%d",&start,&last,&temp);
road[start][last]=road[last][start]=temp;
}
int maxn=dijk(1,n);
sum=0;
int x=n;
fa[sum++]=x;
while(x!=1)
{
for(int i=1;i<=n;i++)
{
if(dist[i]==INT_MAX||road[i][x]==-1)continue;
if(dist[x]==dist[i]+road[i][x])
{
fa[sum++]=i;
x=i;
break;
}
}
}
for(int i=1;i<sum;i++)
{
int s=fa[i];
int t=fa[i-1];
int temp=road[s][t];
road[s][t]=road[t][s]=-1;
maxn=Max(maxn,dijk(1,n));
road[s][t]=road[t][s]=temp;
}
printf("%d
",maxn);
}
return 0;
}