【SPFA】【图论】桐人的约会

P r e f a c e Preface

刀剑神域上周完结了,这周就有题目。。。什么鬼。。。
不过…桐姥爷牛逼!!!

D e s c r i p t i o n Description

这是一个风和日丽的日子,桐人和诗乃在约会(竟然背着亚丝娜约会!) 。他们所在的城市共有N个街区,和M条道路,每条道路连接两个不同的街区,并且通过一条道路需要花费一些时间。他们现在处于N号街区,正在享受幸福时光的桐人完全忘记了他的手机被亚丝娜安装了监控装置的事情,此时亚丝娜已经得知了桐人的位置以及他正在和一个妹子约会的事实,十分愤怒,于是从她所在的1号街区火速赶往N号街区。现在这个城市中有一条道路正在维修,不能通行,不过不论是哪条道路处于维修中,均存在一条路径可以从1号街区前往N号街区,而且亚丝娜一定会选取最短路前往N号街区。现在你很好奇,桐人的美好时光最多还能持续多久,即亚丝娜最多要花费多长的时间才能到达N号街区。

I n p u t Input

第1行:两个正整数N,M,N表示街区个数,M表示道路数。
第2到M+1行 每行三个整数 u,v,w 表示存在一条连接u和v的道路,通过这条道路花费的时间为w
数据保证没有重边和自环

O u t p u t Output

一个整数,表示最多花费的时间。

S a m p l e Sample I n p u t Input

5 7
1 2 8
1 4 10
2 3 9
2 4 10
2 5 1
3 4 7
3 5 10

S a m p l e Sample O u t p u t Output

27

E x p l a i n Explain

30% N<=5, M<=10 
60% N<=1000,M<=10000,w=1
100% N<=1000, M<=N*(N-1)/2,1<=w<=1000

T r a i n Train o f of T h o u g h t Thought

看题目本来以为是一道最长路,但结果是求最短路里面的最长路
题目样例图:

最短路是 1 &gt; 2 &gt; 5 1-&gt;2-&gt;5
然后题目说可以封路
我们就枚举最短路上的每一条边
每次枚举到就把那条边封掉再做SPFA
最后求一个最大和

C o d e Code

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int tt=1,h[1005],dis[1005],maxx;
int n,m,t,r[1005],way[1025]; //r表示最短路上,到这个点的边
bool b[1005],f;
struct node
{
	int now,next,time,last;
}w[8000005];
void SPFA(int xx)
{
	queue<int>q;
	dis[1]=0;
	b[1]=true;
	q.push(1);
	while (q.size())
	{
		int tot=q.front();
		q.pop();
		for (int i=h[tot]; i; i=w[i].next)
		{
			if (i==xx) continue;//封边
			if (dis[w[i].now]>dis[tot]+w[i].time) {
			 dis[w[i].now]=dis[tot]+w[i].time;
			 if (!f)r[w[i].now]=i;
			 if(!b[w[i].now]) 
			 {
			   b[w[i].now]=true;
			   q.push(w[i].now);
			 }
			}
		}
		b[1]=false;
	} 
}
int main()
{
	memset(dis,0x7f,sizeof(dis));
	int x,y,z;
	scanf("%d%d",&n,&m);
	for (int i=1; i<=m; ++i)
	 {
	 	scanf("%d%d%d",&x,&y,&z);
	 	w[++t]=(node){y,h[x],z,x}; h[x]=t;
	 	w[++t]=(node){x,h[y],z,y}; h[y]=t;
	 }
	SPFA(0); 
	f=true;
	tt--;
	int ttt=tt;
	for (int i=n; i!=1; i=w[r[i]].last)
	  {
	  	memset(dis,0x7f,sizeof(dis));
	  	memset(b,false,sizeof(b));
	    SPFA(r[i]);//封边后进行spfa求最短路
	    if (dis[n]!=dis[0]) maxx=max(maxx,dis[n]);//求最大和
	  }
	printf("%d",maxx);
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/LTH060226/article/details/89069391
今日推荐