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 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
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
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
//BY 郭炜老师;
//Bellman-Ford算法,超时;
int F, N, M, W;
const int INF = 1 << 30;
struct Edge
{
int s, e, w;//起点,终点。权;
Edge (int ss, int ee, int ww): s(ss), e(ee), w(ww){};
};
vector<Edge> edges;//储存边;
int dist[510];
int Bellman_Ford(int v) //v是起点;
{
for (int i = 1; i <= N; i++)
{
dist[i] = INF;
}
dist[v] = 0;
for (int k = 1; k < N; k++) //经过不超过k条边;
{// dist(k)[u] = min( dist(k-1)[u], min(dist(k-1)[j] + Edge[j][u])), j = 0, 1, ……, n-1; j不能是终点;
// 类似滚动数组的处理方法;
bool changed = false; //改进;
for (int i = 0; i < edges.size(); i++)
{
int s = edges[i].s;
int e = edges[i].e;
if (dist[s] + edges[i].w < dist[e])
{
dist[e] = dist[s] + edges[i].w; // 从起点v->e的最短路径长度被更新为v->s加上s->e的路径长度之和;
changed = true;
}
}
if (changed == false)
return false;
}
// k == N
for (int i = 0; i < edges.size(); i++)
{
int s = edges[i].s;
int e = edges[i].e;
/*
如果成立,说明找到了经过n条边的v->e的路径,且比任何少于n条边的从v->e的路径都短;
一共n个顶点,路径却经过了n条边,则必有一个顶点m被至少经过两次,则m是一个回路的起点和终点;
走这个回路比不走时路径更短,说明这个回路是负权回路;
*/
if (dist[s] + edges[i].w < dist[e])
return true;
}
return false;
}
int main(){
cin >> F;
while (F--)
{
edges.clear();
cin >> N >> M >> W;
for (int i = 0; i < M; i++)
{
int s, e, t;
cin >> s >> e >> t;
edges.push_back(Edge(s, e, t));
edges.push_back(Edge(e, s, t));
}
for (int i = 0; i < W; i++)
{
int s, e, t;
cin >> s >> e >> t;
edges.push_back(Edge(s, e, -t));
}
if (Bellman_Ford(1)) //因为正权边都是双向,所以从1可达所有点;
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
//SPFA算法是对Bellman-Ford算法的改进。更容易理解;
int F, N, M, W;
const int INF = 1 << 30;
struct Edge{
int e, w; //e表示终点,w表示权值;
Edge(int ee, int ww): e(ee), w(ww){};
};
vector<Edge> G[1000]; //整个有向图;
int updateTimes[1000]; //最短路的改进次数;
int dist[1000]; //dist[i]是源点到i的目前最短路长度;
int spfa(int v) //起点为v;
{
for (int i = 1; i <= N; i++)
dist[i] = INF;
dist[v] = 0;
queue<int> que;
que.push(v);
memset(updateTimes, 0, sizeof(updateTimes));
while (!que.empty())
{
//每次迭代,取出队头的s,依次枚举,看是否存在改进空间;
int s = que.front();
que.pop();
for (int i = 0; i < G[s].size(); i++)
{
int e = G[s][i].e;
if (dist[e] > dist[s] + G[s][i].w) //更新抵达e的最短路径长度;
{
dist[e] = dist[s] + G[s][i].w;
que.push(e); //e有可能改进其它的点,于是将其放入队尾;
updateTimes[e]++;
/*
若一个点最短路被改进的次数达到n,则存在负权环;
其原因与Bellma-Ford算法是类似的;
n次改进说明找到了一条经过n条边的从源点s到特定点e的路径;
一共只有n个点,说明e是一个环的起点和终点;
走这个回路比不走这个回路路径更短,说明它是负权回路;
*/
if (updateTimes[e] >= N)
return true;
}
}
}
return false;
}
int main()
{
cin >> F;
while (F--)
{
cin >> N >> M >> W;
for (int i = 1; i < 1000; i++)
G[i].clear();
int s, e, t;
for (int i = 0; i < M; i++)
{
cin >> s >> e >> t;
G[s].push_back(Edge(e, t));
G[e].push_back(Edge(s, t));
}
for (int i = 0; i < W; i++)
{
cin >> s >> e >> t;
G[s].push_back(Edge(e, -t));
}
if (spfa(1))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}