HDU1595:find the longest of the shortest(Dijkstra)
2014-08-06 16:31
453 查看
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
一开始还以为直接枚举删边就可以了,没想到TLE(5s都超。。),
认真的想了一下,只有原来最短路相关的边不能通行,才真正需要改走其他路,重新求最短路,
而跟原来最短路无关的边不能通行了,就无需改走其他路,直接就是走原来的最短路了,
所以用枚举删边的暴力方法的话,删除各条与原来最短路无关的边时,就会做很多重复且没必要的运算,浪费了大量时间
先走一次Dijkstra,用邻接表的方式保存最短路路径,然后再根据邻接表删边求最短路,
保留这个最大值。
#include<stdio.h>
#include<algorithm>
#define inf 9999999
int map[1005][1005];
int vis[1005],di[1005],link[1005];
int n,m;
void dij(int flag)
{
memset(vis,0,sizeof(vis));
int i,j,min,pos;
for(i=1;i<=n;i++)
di[i]=inf;
di[1]=0;
for(i=1;i<n;i++)
{
min=inf;
for(j=1;j<=n;j++)
if(!vis[j]&&min>di[j])
{
min=di[j];
pos=j;
}
vis[pos]=1;
for(j=1;j<=n;j++)
if(!vis[j]&&di[j]>di[pos]+map[pos][j])
{
di[j]=di[pos]+map[pos][j];
if(flag)
link[j]=pos;//保存路径且只让运行一次
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<=n;i++)
{
map[i][i]=0;
for(int j=0;j<=n;j++)
map[i][j]=inf;
}
int u,v,w;
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
if(w<map[u][v])
map[v][u]=map[u][v]=w;
}
memset(link,0,sizeof(link));
dij(1);
int ans=di
;
for(int i=n;i!=1;i=link[i])
{
int tem=map[i][link[i]];
map[i][link[i]]=map[link[i]][i]=inf;
dij(0);
ans=ans>di
?ans:di
;
map[i][link[i]]=map[link[i]][i]=tem;
}
printf("%d\n",ans);
}
return 0;
}
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
一开始还以为直接枚举删边就可以了,没想到TLE(5s都超。。),
认真的想了一下,只有原来最短路相关的边不能通行,才真正需要改走其他路,重新求最短路,
而跟原来最短路无关的边不能通行了,就无需改走其他路,直接就是走原来的最短路了,
所以用枚举删边的暴力方法的话,删除各条与原来最短路无关的边时,就会做很多重复且没必要的运算,浪费了大量时间
先走一次Dijkstra,用邻接表的方式保存最短路路径,然后再根据邻接表删边求最短路,
保留这个最大值。
#include<stdio.h>
#include<algorithm>
#define inf 9999999
int map[1005][1005];
int vis[1005],di[1005],link[1005];
int n,m;
void dij(int flag)
{
memset(vis,0,sizeof(vis));
int i,j,min,pos;
for(i=1;i<=n;i++)
di[i]=inf;
di[1]=0;
for(i=1;i<n;i++)
{
min=inf;
for(j=1;j<=n;j++)
if(!vis[j]&&min>di[j])
{
min=di[j];
pos=j;
}
vis[pos]=1;
for(j=1;j<=n;j++)
if(!vis[j]&&di[j]>di[pos]+map[pos][j])
{
di[j]=di[pos]+map[pos][j];
if(flag)
link[j]=pos;//保存路径且只让运行一次
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<=n;i++)
{
map[i][i]=0;
for(int j=0;j<=n;j++)
map[i][j]=inf;
}
int u,v,w;
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
if(w<map[u][v])
map[v][u]=map[u][v]=w;
}
memset(link,0,sizeof(link));
dij(1);
int ans=di
;
for(int i=n;i!=1;i=link[i])
{
int tem=map[i][link[i]];
map[i][link[i]]=map[link[i]][i]=inf;
dij(0);
ans=ans>di
?ans:di
;
map[i][link[i]]=map[link[i]][i]=tem;
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- hdu1595 find the longest of the shortest(dijkstra+枚举)
- hdu1595 find the longest of the shortest(Dijkstra)
- hdu1595 find the longest of the shortest (dijkstra+记录路径)
- hdu1595 find the longest of the shortest(Dijkstra)
- HDU1595:find the longest of the shortest(Dijkstra)
- hdu 1595 find the longest of the shortest (dijkstra)
- HDU1595---find the longest of the shortest
- hdu1595-find the longest of the shortest
- HDU1595 find the longest of the shortest(最短路)
- HDU1595 find the longest of the shortest
- hdu1595(spfa) find the longest of the shortest
- HDU1595 find the longest of the shortest
- hdu 1595 find the longest of the shortest(dijkstra)
- hdu 1595 find the longest of the shortest(dijkstra)
- hdu 1595 find the longest of the shortest (Dijkstra+路径处理)
- hdu 1595 find the longest of the shortest (dijkstra + spfa)
- HDU1595 - find the longest of the shortest - 最短路变形+暴力
- HDU 1595 find the longest of the shortest (Dijkstra)
- HDU 1595 find the longest of the shortest(Dijkstra)
- HDU1595:find the longest of the shortest(spfa+记录路径)