HDU 1595find the longest of the shortest(spfa)

本文介绍了一种利用SPFA算法优化最短路径计算的方法。通过记录路径并移除关键边来找出次优路径,适用于解决特定类型的问题,如寻找从城市A到城市B的第二短路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
 

Author
ailyanlu
 

Source
 

Recommend
8600   |   We have carefully selected several similar problems for you:   1599  1596  1142  1217  1597 

题目意思:先求1到n最短路,然后删除一条边,求得的第二长的1到n最短路的值?


思路:spfa的过程中记录路径,然后删除最短路中的边求得答案



#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

#define N 1005
#define INF 0x3f3f3f3f

int n,m,vis[N],use[N][N];
int head[N],dis[N];
int num,pre[N];

struct stud{
int to,next;
int va;
}e[N*N];

void build(int u,int v,int va)
{
	e[num].to=v;
	e[num].va=va;
	e[num].next=head[u];
	head[u]=num++;
}

void spfa()
{
	int i,j;
	queue<int>q;
	memset(dis,INF,sizeof(dis));
	memset(vis,0,sizeof(vis));
	vis[1]=1;
	dis[1]=0;
	q.push(1);
	pre[1]=-1;
	int cur;
	while(!q.empty())
	{
		cur=q.front();
		q.pop();
		vis[cur]=0;
		for(i=head[cur];i!=-1;i=e[i].next)
		{
			int x=e[i].to;
			if(dis[x]>dis[cur]+e[i].va)
			{
				dis[x]=dis[cur]+e[i].va;
				pre[x]=cur;
				if(!vis[x])
				{
					vis[x]=1;
					q.push(x);
				}
			}
		}
	}
}

int work()
{
	int i,j;
	memset(dis,INF,sizeof(dis));
	memset(vis,0,sizeof(vis));
	queue<int>q;
	q.push(1);
	vis[1]=1;
	dis[1]=0;
	int cur;

	while(!q.empty())
	{
		cur=q.front();
		q.pop();
		vis[cur]=0;
		for(i=head[cur];i!=-1;i=e[i].next)
		{
			int x=e[i].to;
			if(use[cur][x]&&dis[x]>dis[cur]+e[i].va)
			{
				dis[x]=dis[cur]+e[i].va;
				if(!vis[x])
				{
					q.push(x);
					vis[x]=0;
				}
			}
		}
	}
	return dis[n];
}

int main()
{
	int i,j,u,v,va;
	while(~scanf("%d%d",&n,&m))
	{
		memset(use,0,sizeof(use));
		memset(head,-1,sizeof(head));
		num=0;
		while(m--)
		{
			scanf("%d%d%d",&u,&v,&va);
			use[u][v]=use[v][u]=1;
			build(u,v,va);
			build(v,u,va);
		}
		spfa();
		int ans=dis[n];

		for(i=n;pre[i]!=-1;i=pre[i])
		{
			int x=pre[i];
			use[i][x]=use[x][i]=0;
			ans=max(work(),ans);
			use[i][x]=use[x][i]=1;
		}
		printf("%d\n",ans);
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值