• poj 3259 Wormholes


    题意:就是有N个,M条道路,W条时间隧道,问是否能从某一点出经过一些道路和时间隧道后再次到达该顶点并且回到该点过去

    思路:最短路,只要有负权回路即可判断能回到过去

    注意,道路是双向的所以有2500*2条,再加上单向的W200条,贡献一次RE

    /*
    spfa()
    2011-8-16
    */
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <climits>
    #include <algorithm>
    #include <functional>
    #include <cstdlib>
    #include <queue>
    #include <vector>
    #include <stack>
    #include <cmath>
    #define nMax 505
    #define mMax 5505
    #define INF INT_MAX
    using namespace std;
    struct Edge
    {
    int u, v, w;
    }edge[mMax];
    int head[nMax], dist[nMax], cnt[nMax];
    bool used[nMax];
    int e, n, m, w;

    void BuildGraph(int from, int to, int weight)
    {
    edge[e].v=to;
    edge[e].w=weight;
    edge[e].u=head[from];
    head[from]=e++;
    }

    void spfa()
    {
    memset(used, 0, sizeof(used));
    memset(cnt, 0, sizeof(cnt));
    queue<int> que;

    for(int i=2; i<=n; i++) dist[i]=INF;
    dist[1]=0;
    used[1]=1;

    que.push(1);
    while(!que.empty())
    {
    int u=que.front();
    que.pop();
    used[u]=0;

    for(int i=head[u]; i!=-1; i=edge[i].u)
    {
    int v=edge[i].v;
    if(edge[i].w+dist[u]<dist[v])
    {
    dist[v]=edge[i].w+dist[u];
    if(!used[v])
    {
    used[v]=1;
    que.push(v);
    cnt[v]++;
    if(cnt[v]>n)
    {
    puts("YES");
    return ;
    }
    }
    }
    }
    }
    puts("NO");
    }

    int main()
    {
    int ntime;
    scanf("%d", &ntime);
    while(ntime--)
    {
    int s, o, t;
    e=0;
    memset(head, -1, sizeof(head));
    scanf("%d%d%d", &n, &m, &w);
    //cout<<" "<<n<<" "<<m<<" "<<w<<endl;
    for(int i=0; i<m+w; i++)
    {
    scanf("%d%d%d", &s, &o, &t);
    if(i>=m) BuildGraph(s, o, -t);
    else
    {
    BuildGraph(s, o, t);
    BuildGraph(o, s, t);
    }
    }
    spfa();
    }
    return 0;
    }
  • 相关阅读:
    编译C语言单元测试框架CUnit库的方法
    C# 基本元素
    pip的基本使用
    Ubuntu源码安装php
    Ubuntu源码安装Apache服务器
    centos7搭建LAMP(yum安装)
    windows搭建web环境(WAMP)
    windows配置web环境之(搭建php7+apache2 )
    安装thinkphp5
    分离式lnmp部署
  • 原文地址:https://www.cnblogs.com/FreeAquar/p/2140384.html
Copyright © 2020-2023  润新知