腾讯大战360【SPFA】

>Description
现在,腾讯与360由于身处异地,非常迫切地想在最短的时间内相遇,他们希望你来帮他们找到一个最好的方案使得相遇的时间最短。
在此我们定义“相遇”为:两个人皆在同一个编号的城市上就可以了。也就是说,在这里我们不考虑两人在路中间相遇。


>Input
输入数据第一行:N和M(用空格隔开) 表示这是一个N*N的图并且有M条边,第二行到第M+1行 为这个图的详细信息。
每行共有被空格隔开的三个数:a b c。表示编号为a的城市到编号为b的城市
有一个双向边,并且要过这条双向边所需要花费的时间为c。
最后一行有两个数:S和T,S表示腾讯所处的城市(也就是深圳),T表示360所处的
城市(也就是北京)

>Output
输出只有一行,D,表示二者“相遇”的最短时间。当然,如果无法相遇则输出“Peace!”


>Sample Input
3 3
1 2 1
2 3 1
1 3 1
1 3

>Sample Output
1


>解题思路
[○・`Д´・ ○]
由于这该死的可爱的数组范围和这该死的可爱的cstring头文件,所以我拿了0分。

其实很简单,就是用两个spfa分别算出s(腾讯)到每个点的最短路和t(360)到每个点的最短路,然后再枚举s和t相遇的点,求出最短的用时。


>代码

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct ooo
{
	int to,next;
}f[50005];
int ans,n,m,x,y,z,s,t,tt,h[5005],a[5005][5005],c[5005],cc[5005],st[15005],head,tail;
bool yy[5005];
int main()
{
	memset(c,0x7f,sizeof(c));
	memset(cc,0x7f,sizeof(cc));
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&x,&y,&z);
		f[++tt].to=y; f[tt].next=h[x]; h[x]=tt;
		f[++tt].to=x; f[tt].next=h[y]; h[y]=tt; //无向图
		a[x][y]=a[y][x]=z; //记录权值
	}
	scanf("%d%d",&s,&t);
	
	c[s]=0; st[1]=s; yy[s]=1;
	head=0; tail=1;
	do
	{
		head++;
		for(int i=h[st[head]];i;i=f[i].next)
		 if(c[st[head]]+a[st[head]][f[i].to]<c[f[i].to])
		 {
		 	c[f[i].to]=c[st[head]]+a[st[head]][f[i].to];
		 	if(!yy[f[i].to])
		 	{
		 		st[++tail]=f[i].to;
		 		yy[f[i].to]=1;
		 	}
		 }
		yy[st[head]]=0;
	}while(head<tail); //先算出s的
	
	cc[t]=0; st[1]=t; yy[t]=1;
	head=0; tail=1;
	do
	{
		head++;
		for(int i=h[st[head]];i;i=f[i].next)
		 if(cc[st[head]]+a[st[head]][f[i].to]<cc[f[i].to])
		 {
		 	cc[f[i].to]=cc[st[head]]+a[st[head]][f[i].to];
		 	if(!yy[f[i].to])
		 	{
		 		st[++tail]=f[i].to;
		 		yy[f[i].to]=1;
		 	}
		 }
		yy[st[head]]=0;
	}while(head<tail); //再算出t的
	
	ans=c[0];
	for(int i=1;i<=n;i++)
	 ans=min(ans,max(c[i],cc[i])); //最短用时
	if(ans!=c[0]) printf("%d",ans);
	else printf("Peace!"); //如果不能相遇
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值