POJ3259 FLOYD

来源:互联网 发布:手机健身运动软件 编辑:程序博客网 时间:2024/06/08 00:26

2017年3月28日 | ljfcnyali
题目大意
农夫约翰在探索他的许多农场,发现了一些惊人的虫洞。虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径,W(1≤W≤200)个虫洞。FJ作为一个狂热的时间旅行的爱好者,他要做到以下几点:开始在一个区域,通过一些路径和虫洞旅行,他要回到最开时出发的那个区域出发前的时间。也许他就能遇到自己了:)。为了帮助FJ找出这是否是可以或不可以,他会为你提供F个农场的完整的映射到(1≤F≤5)。所有的路径所花时间都不大于10000秒,所有的虫洞都不大于万秒的时间回溯。

Sample Input

23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8

Sample Output

NOYES

题目分析
思路很简单,只要判断是否存在负权环就行了,用floyd一次搞定!

陷阱提醒
小心min函数,我因为加了min函数而超时了

AC代码

/*************************************************************************    > File Name: POJ3259.cpp    > Author: ljf-cnyali    > Mail: ljfcnyali@gmail.com     > Created Time: 2017/3/28 19:03:15 ************************************************************************/#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#include<map>#include<set>#include<vector>#include<queue>using namespace std;#define REP(i, a, b) for(int i = (a), _end_ = (b);i <= _end_; ++ i)#define mem(a) memset((a), 0, sizeof(a))#define str(a) strlen(a)const int maxn = 510;int n, m, k;int Map[maxn][maxn];bool Floyd() {    REP(k, 1, n)        REP(i, 1, n) {            REP(j, 1, n) {                int t = Map[i][k] + Map[k][j];                if(Map[i][j] > t)                    Map[i][j] = t;            }            if(Map[i][i] < 0)                return true;        }    return false;}int main() {#ifndef ONLINE_JUDGE    freopen("input.txt", "r", stdin);    freopen("output.txt", "w", stdout);#endif    int T;    scanf("%d", &T);        while(T --) {        scanf("%d%d%d", &n, &m, &k);        memset(Map, 0x3f3f3f3f, sizeof(Map));        REP(i, 1, n)            Map[i][i] = 0;        int x, y, w;        REP(i, 1, m) {            scanf("%d%d%d", &x, &y, &w);            if(w < Map[x][y])                Map[x][y] = Map[y][x] = w;        }        REP(i, 1, k) {            scanf("%d%d%d", &x, &y, &w);            Map[x][y] = -w;        }        if(Floyd())            printf("YES\n");        else            printf("NO\n");    }    return 0;}

本文转自:http://ljf-cnyali.cn/index.php/archives/115