poj - 3259 spfa + 多源点 + 判负环

题意:FJ有F个农场,每个农场有N块田,N条路,W个虫洞。

           每条路表示a, b 之间有一条路耗时c,每个虫洞表示从a到b走会回到c时间之前。

           问FJ是否能找到一条路回到他的出发点时间之前见到自己,出发点可任选。

虫洞实际上就是从a到b有一条耗时为-c的路,问的就是有没有花费时间为负数的环路

即用spfa找负环,不过起点未定要遍历虫洞的终点或起点去跑spfa

另外可以建立一个超级源连向这些起点跑一遍spfa,写的时候没想到 orz  应该会更快 。。。。。。

链接:poj 3259

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#define inf 0x3f3f3f3f

using namespace std;

const int maxn = 1005;
const int maxm = 1000500;

int n, m, s, t;   //n为点数 s为源点
int head[maxn]; //head[from]表示以head为出发点的邻接表表头在数组es中的位置,开始时所有元素初始化为-1
int d[maxn]; //储存到源节点的距离,在Spfa()中初始化
int cnt[maxn];
bool inq[maxn]; //这里inq作inqueue解释会更好,出于习惯使用了inq来命名,在Spfa()中初始化
int nodep;  //在邻接表和指向表头的head数组中定位用的记录指针,开始时初始化为0
int pre[maxn];

struct node {
    int v, w, next;
}es[maxm];

void init() {
    for(int i = 1; i <= n; i++) {
        d[i] = inf;
        inq[i] = false;
        cnt[i] = 0;
        head[i] = -1;
        pre[i] = -1;
    }
    nodep = 0;
}

void addedge(int from, int to, int weight)
{
    es[nodep].v = to;
    es[nodep].w = weight;
    es[nodep].next = head[from];
    head[from] = nodep++;
}

bool spfa()
{
    queue<int> que;
    d[s] = 0;    //s为源点
    inq[s] = 1;
    que.push(s);
    while(!que.empty()) {
        int u = que.front();
        que.pop();
        inq[u] = false;   //从queue中退出
        //遍历邻接表
        for(int i = head[u]; i != -1; i = es[i].next) {  //在es中,相同from出发指向的顶点为从head[from]开始的一项,逐项使用next寻找下去,直到找到第一个被输
                                                        //入的项,其next值为-1
            int v = es[i].v;
            if(d[v] > d[u] + es[i].w) { //松弛(RELAX)操作
                d[v] = d[u] + es[i].w;
                //pre[v] = u;
                if(!inq[v]) {      //若被搜索到的节点不在队列que中,则把to加入到队列中去
                    inq[v] = true;
                    que.push(v);
                    if(++cnt[v] > n) {
                        return false;
                    }
                }
            }
        }
    }
    return true;
}

void putpath() {
    stack<int> path;
    int now = t;
    while(1) {
        path.push(now);
        if(now == s) {
            break;
        }
        now = pre[now];
    }
    while(!path.empty()) {
        now = path.top();
        path.pop();
        printf("%d\n", now);
    }
}

int mp[maxn][maxn];
int f[maxn];

int main()
{
    int T, kcase = 0;
    cin >> T;
    int w;
    while(T--) {
        cin >> n >> m >> w;
        memset(mp, inf, sizeof(mp));
        memset(f, 0, sizeof(f));
        int a, b, c;
        while(m--) {
            cin >> a >> b >> c;
            if(mp[a][b] > c){
                mp[a][b] = mp[b][a] = c;
            }
        }
        while(w--) {
            cin >> a >> b >> c;
            if(mp[a][b] > -c) {
                mp[a][b] = -c;
                f[b] = 1;
            }
        }
        init();
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                if(mp[i][j] != inf) {
                    addedge(i, j, mp[i][j]);
                }
            }
        }
        int flag = 0;
        for(int i = 1; i <= n; i++) {
            if(f[i]) {
                for(int j = 1; j <= n; j++) {
                    d[j] = inf;
                    inq[j] = false;
                    cnt[j] = 0;
                }
                s = i;
                if(!spfa()) {
                    flag = 1;
                    break;
                }
            }
        }
        if(flag) {
            puts("YES");
        }
        else {
            puts("NO");
        }

    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值