zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Maven安装及配置
    Java部分概念理解
    API.day01
    随机生成10元素数组并找出最大元素(Java)
    冒泡排序(Java)
    俄罗斯方块部分功能(Java)
    判断闰年(Java)
    判断质数(Java)
    基于DSP的IS95正向业务信道模块设计
    Lua程序设计(4th) 第一部分 语言基础
  • 原文地址:https://www.cnblogs.com/FreeAquar/p/2140384.html
Copyright © 2011-2022 走看看