Time Limit: 2000MS
Memory Limit: 65536KTotal Submissions: 48628 Accepted: 17925Description
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 8Sample Output
NO YESHint
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.
题目大意: 给你一个图 图中边的权值如果是路径的话 这表示从一个顶点到另一个顶点花费的时间t,如果是虫洞的话边的权值就表示一个点到另一个点可以回到t秒之前
解题思路:简单的一道bellman的水题,判断这道题有无负环要注意以下几点:
1.M+2..M+W+1(这几行表示虫洞,复制的题目line2的说明,不懂的怎么来的话可以看题目)中的的路径值要写成负数,因为它表示的 假如 输入 2 3 9 则表示的是 从顶点2到顶点3有一个虫洞可以回到9秒之前
2. path 是双向的,但是虫洞是单项的,所以开边的数组最少要开 2*2500+200 不然POJ会 Runtime Error (都是泪的教训)
#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int MAXN = 505; const int INF = 0x3f3f3f3f; struct Edge{ int start; int to; int cost; }edge[MAXN * 10]; /* 因为输入边用的较多,且代码都一样所以我就定义了一个输入函数 关于内联函数(lnline)的介绍可以看这篇博客:https://www.cnblogs.com/fnlingnzb-learner/p/6423917.html 我就是单纯的为了用一下,能不能节省时间我也不知道,反正加不加 inline 都可以AC */ void input(int s, int e, int t, int& edgeNum) { edge[edgeNum].start = s; edge[edgeNum].to = e; edge[edgeNum].cost = t; edgeNum++; } bool find_negative_loop(int n, int edgeNum) //判断有无负环,n: 定点数 edgeNum: 边数 { int d[MAXN]; //最短距离 /* 这里d数组 初始化为0,因为每个顶点之间都有可能存在负环 其实也可以初始化为INF,因为判断负环是根据遍历顶点的次数 判断的并不是距离,按理说可以初始化成任何数,但是为了代码 规范还是0比较好,不排除有特殊情况初始化成INF */ memset(d, 0, sizeof(d)); for (int i = 0; i < n; i++) { for (int j = 0; j < edgeNum; j++) { Edge e = edge[j]; if (d[e.to] > d[e.start] + e.cost) { d[e.to] = d[e.start] + e.cost; if (i == n - 1) { //如果第n次依然更新了,则存在负圈 return true; } } } } return false; } int main() { int t; cin >> t; while (t--) { int n, m, w; int edgeNum = 0; edgeNum = 0; cin >> n >> m >> w; int s, e, t; for (int i = 0; i < m; i++) { //输入路径 cin >> s >> e >> t; input(s, e, t, edgeNum); input(e, s, t, edgeNum); } for (int i = 0; i < w; i++) { //输入虫洞 cin >> s >> e >> t; input(s, e, -t, edgeNum); } if (find_negative_loop(n, edgeNum)) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0; }