农夫约翰在探索他的许多农场,发现了一些惊人的虫洞。虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径,W(1≤W≤200)个虫洞。FJ作为一个狂热的时间旅行的爱好者,他要做到以下几点:开始在一个区域,通过一些路径和虫洞旅行,他要回到最开时出发的那个区域出发前的时间。也许他就能遇到自己了:)。为了帮助FJ找出这是否是可以或不可以,他会为你提供F个农场的完整的映射到(1≤F≤5)。所有的路径所花时间都不大于10000秒,所有的虫洞都不大于万秒的时间回溯。
Input
第1行:一个整数F表示接下来会有F个农场说明。 每个农场第一行:分别是三个空格隔开的整数:N,M和W 第2行到M+1行:三个空格分开的数字(S,E,T)描述,分别为:需要T秒走过S和E之间的双向路径。两个区域可能由一个以上的路径来连接。 第M +2到M+ W+1行:三个空格分开的数字(S,E,T)描述虫洞,描述单向路径,S到E且回溯T秒。
Output
F行,每行代表一个农场 每个农场单独的一行,” YES”表示能满足要求,”NO”表示不能满足要求。
Sample Input
2 3 3 1 1 2 2 1 3 4 2 3 1 3 1 3 3 2 1 1 2 3 2 3 4 3 1 8
Sample Output
NO YES
Hint
For farm 1, FJ cannot travel back in time.
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.
思路:
题目其实并不是很难 ,第一次作的时候还被吓到了(因为刚学最短路),自己被自己骗到了;
本题让判断是否能回到最开始的区域并且能否遇到出发前的自己,说的很绕口,其实就是寻找最短路里面存不存在负环,只要存在那么必然可以一直走负环,从而回到开始之前的时间
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<map>
#include<cstring>
#include<queue>
#include<set>
#include<stdlib.h>
using namespace std;
typedef long long ll;
//#define dbug cout<<"hear!"<<erndl;
const int N = 2e5 + 10, INF = 0x3f3f3f3f;
int k,n, m,cnt,w;
int arr[505][505];
int dis[N];
struct node
{
int a, b;
int w;
}nodee[N];
int bell()
{
for (int i = 1;i <= n;i++)
{
dis[i] = INF;
}
dis[1] = 0;
int flag ;
for (int i = 1;i < n;i++)
{
flag = 0;
for (int j = 0;j < cnt;j++)
{
if (dis[nodee[j].b] > dis[nodee[j].a] + nodee[j].w)
{
dis[nodee[j].b] = dis[nodee[j].a] + nodee[j].w;
flag = 1;
}
}
if (flag == 0)
{
break;
}
}
return flag;
}
int main()
{
int t;
cin >> t;
while (t--)
{
cnt = 0;
cin >> n >> m >> w;
while (m--)
{
int a, b, c;
cin >> a >> b >> c;
nodee[cnt].a = a, nodee[cnt].b = b, nodee[cnt].w = c;
cnt++;
nodee[cnt].b = a, nodee[cnt].a = b, nodee[cnt].w = c;
cnt++;
}
while (w--)
{
int a, b, c;
cin >> a >> b >> c;
nodee[cnt].a = a, nodee[cnt].b = b, nodee[cnt].w = -c;
cnt++;
}
int ans = bell();
if (ans == 0)
{
cout << "NO" << endl;
}
else
{
int check = 1;
for (int i = 0;i < cnt;i++)//此处为ballman-ford判断负环的方法
{
if (dis[nodee[i].b] > dis[nodee[i].a] + nodee[i].w)
{
check = 1;
break;
}
}
if (check == 1)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
}
}