您的位置:首页 > 其它

hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)

2017-07-17 13:25 627 查看

find the longest of the shortest

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

Sampl
4000
e Output

11

13

27

题意:删除一条边,使得剩下边组成的最短距离最长

思路:既然要删除边,如果我们枚举m条边进行删除的话,妥妥的超时(没毛病。)

那么我们应该怎样删呢?

既然是要使得剩下边组成的最短距离最长,那么删除的这条边肯定对最短距离有影响。

因此我们只需要先求出最短距离,然后对最短距离上的边进行删除即可

代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;

const int inf=0x3f3f3f3f;
const int maxn=1000+10;
const int maxv=1000000;

struct edge//存边
{
int v,w,next;
} E[maxv];
struct node//记录m条边
{
int u,v,w;
} e[maxv];
int inq[maxn],first[maxn],d[maxn];
int w[maxn],link[maxn][maxn],pre[maxn];
int n,m,len,destroy_u,destroy_v,ps;

void spfa()
{
memset(inq,0,sizeof(inq));
memset(d,inf,sizeof(d));
int st=1;
queue<int>q;
q.push(st);
inq[st]=1,d[st]=0;
while(!q.empty())
{
st=q.front();
inq[st]=0,q.pop();
for(int i=first[st]; ~i; i=E[i].next)
{
int v=E[i].v,w=E[i].w;
if((st!=destroy_u||v!=destroy_v)&&(st!=destroy_v||v!=destroy_u)&&d[v]>d[st]+w)
{
pre[v]=st;//记录路径
d[v]=d[st]+w;
if(!inq[v])
{
inq[v]=1;
q.push(v);
}
}
}
}
}

void print(int x)//打印最短路径
{
if(pre[x]!=-1)
{
print(pre[x]);
w[ps++]=x;
}
}

void add_edge(int u,int v,int w)//邻接表存边
{
E[len].v=v,E[len].w=w,E[len].next=first[u],first[u]=len++;
}

int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(pre,-1,sizeof(pre));
memset(link,0,sizeof(link));
memset(first,-1,sizeof(first));
len=0;
for(int i=0; i<m; ++i)
{
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
add_edge(e[i].u,e[i].v,e[i].w);
add_edge(e[i].v,e[i].u,e[i].w);
}
destroy_u=destroy_v=0,pre[1]=-1,ps=0;
spfa();
int ans=d
;
w[ps++]=1;
print(n);
for(int i=0; i<ps-1; ++i)//记录最短路径上的所有边
link[w[i]][w[i+1]]=link[w[i+1]][w[i]]=1;
for(int i=0; i<m; ++i)
{
destroy_u=e[i].u,destroy_v=e[i].v;
if(link[destroy_u][destroy_v])//只对最短路径上的边进行删除
{
spfa();
if(d
!=inf)
ans=max(ans,d
);
}
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐