POJ3259 FLOYD

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

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

题目分析
思路很简单,只要判断是否存在负权环就行了,用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值