Description
While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ's farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.
As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .
To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.
Input
Line 1: A single integer, F. F farm descriptions follow.
Line 1 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2..M+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
Lines M+2..M+W+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.
Output
Lines 1..F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).
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.
题意:
就是让你判断是否有负环回路,
思路:
Bellman_ford算法
与Dijkstra相似,Bellman-ford也可以求出单源的最短路径,但是bellman_ford比Dijkstra好的是,Bellman_ford可以判断出路径中是否有负回环路。
Dijkstra最重要的一步就是松弛操作,应为Dijkstra处理的数据都是正数,所以它的松弛操作是有极限的,但如果遇到负的回环路,它的松弛可以无限松弛下去,那么Bellman_ford最经典的操作来了,它把Dijkstra的松弛定个极限,当跳出这个极限后,如果还会出现可以松弛的情况,那么就是存在负回环路。
Bellman_ford的松弛类似dp前面的松弛结果可能对后面的松弛有影响,值得注意
判断是否有负环形路时,初始的s值为1即可,不用循环
bellman_ford最主要的功能还是求出源点到各个点的最短路径,而在它更新最短路径(就是dis[]),发现了有负回环路时,直接跳出,表明这个图不能求出最短路径,所以说发现负回环路只是它附带的一个产物 XD ,所以无论源点s为何值都可以发现这个负回环路
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
struct ac{
int s, e, dis;
ac() {}
ac (int &_s, int &_e, int &_dis) {
s = _s; e = _e; dis = _dis;
}
};
vector<ac> vv;
int n, m, w;
const int INF = 0x3f3f3f3f;
int dis[505];
bool belmanford(int s) {
memset(dis, INF, sizeof(dis));//初始化dis全为INF
dis[s] = 0;//把起始点赋值为0
for (int i = 1; i <= n; i ++) {//从第一个点到最后一个点Dijkstra最多松弛n次
bool flag = false;//初始化默认不能在松弛了
for (int j = 0; j < vv.size(); j ++) {//把每个边都看能否进行松弛,类似Dijkstra把关联的路径松弛
int ss = vv[j].s;
int ee = vv[j].e;
int diss = vv[j].dis;
if (dis[ee] > dis[ss] + diss) {
dis[ee] = dis[ss] + diss;
flag = true;//能松弛变成true
}
}
if (!flag)//如果经过上面松弛发现全都不能在松弛了,那么Dijkstra 已经走完,直接break
break;
}
for (int j = 0; j < vv.size(); j ++) {//如果上面不是通过flag退出循环的,那么就再松弛一次,如果能松弛那么表示有负回环路
int ss = vv[j].s;
int ee = vv[j].e;
int diss = vv[j].dis;
if (dis[ee] > dis[ss] + diss) {
dis[ee] = dis[ss] + diss;
return true;//有负回环路返回true
}
}
return false;
}
int main() {
int T;
cin >> T;
while(T--) {
vv.clear();
cin >> n >> m >> w;
for (int i = 0; i < m; i ++){
int s, e, dis;
cin >> s >> e >> dis;
vv.push_back(ac(s, e, dis));
vv.push_back(ac(e, s, dis));
}
for (int i = 0; i < w; i ++) {
int s, e, dis;
cin >> s >> e >> dis;
dis *= (-1);
vv.push_back(ac(s, e, dis));
}
int f = 0;
for (int i = 1; i <= n; i ++) {
if (belmanford(i)) {
f = 1;
break;
}
}
if (f)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
SPFA算法
SPFA的思想与Bellman_ford相同,都是模拟了Dijkstra松弛的操作,因为每个点最多松弛n次如果发现有点松弛了超过n次那么肯定存在负环回路
#include <iostream>
#include <vector>
#include <string.h>
#include <queue>
using namespace std;
struct ac{
int e, dis;
ac() {}
ac (int &_e, int &_dis) {
e = _e; dis = _dis;
}
};
vector<ac> vv[305];
int n, m, w;
const int INF = 0x3f3f3f3f;
int dis[505];
int c[305], vis[305];
bool SPFA(int s) {
queue<int> q;
memset(dis, INF, sizeof(dis));
dis[s] = 0;//起始点
memset(vis, 0, sizeof(vis));//全赋初值
memset(c, 0, sizeof(c));
q.push(s);
vis[s] = 1;//如队列就标记
c[s] = 1;//记录次数
while(!q.empty()) {
int x;
x = q.front(); q.pop();//队列头拿出
vis[x] = 0;//取消标记
for (int j = 0; j < vv[x].size(); j ++) {//松弛相关点
ac t = vv[x][j];
if (dis[t.e] > dis[x] + t.dis) {
dis[t.e] = dis[x] + t.dis;
if (!vis[t.e]) {//如果此时这个点不在队列中,如队
vis[t.e] = 1;//并标记
c[t.e] ++;//次数++
q.push(t.e);//如对
if (c[t.e] > n) {//如果发现大于n次了,返回true
return true;
}
}
}
}
}
return false;
}
int main() {
int T;
cin >> T;
while(T--) {
for (int i = 1; i < 305; i ++) {
vv[i].clear();
}
cin >> n >> m >> w;
for (int i = 0; i < m; i ++){
int s, e, dis;
cin >> s >> e >> dis;
vv[s].push_back(ac(e, dis));
vv[e].push_back(ac(s, dis));
}
for (int i = 0; i < w; i ++) {
int s, e, dis;
cin >> s >> e >> dis;
dis *= (-1);
vv[s].push_back(ac(e, dis));
}
int f = 0;
if (SPFA(1))
f = 1;
if (f)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}