zoukankan      html  css  js  c++  java
  • UVA 558 判定负环,spfa模板题

    1、UVA 558 Wormholes   

    2、总结:第一个spfa,好气的是用next[]数组判定Compilation error,改成nexte[]就过了。。难道next还是特殊词吗

    题意:科学家,虫洞。就是判定负环

    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<algorithm>
    #include<cstdio>
    #define F(i,a,b) for (int i=a;i<b;i++)
    #define FF(i,a,b) for (int i=a;i<=b;i++)
    #define mes(a,b) memset(a,b,sizeof(a))
    #define INF 0x3f3f3f3f
    #define LL long long
    using namespace std;
    const int N=1010,M=2010,MAX=1000100;
    
    int n,m;
    int v[M],u[M],w[M],nexte[M],head[N],num[N],dis[N],vis[N];
    
    void read_gragh()
    {
        mes(head,-1);
        scanf("%d%d",&n,&m);
        FF(e,1,m){
            scanf("%d%d%d",&u[e],&v[e],&w[e]);
            nexte[e]=head[u[e]];
            head[u[e]]=e;
        }
    }
    
    bool spfa()
    {
        mes(vis,0);mes(num,0);
        mes(dis,INF);
        dis[0]=0;
        queue<int>q;q.push(0);
        num[0]++;
        while(!q.empty())
        {
            int node=q.front();q.pop();
            vis[node]=0;
            for(int e=head[node];e!=-1;e=nexte[e]){
                if(dis[v[e]]>dis[node]+w[e]){
                    dis[v[e]]=dis[node]+w[e];
                    if(!vis[v[e]]){
                        vis[v[e]]=1;
                        q.push(v[e]);
                        num[v[e]]++;
                        if(num[v[e]]>=n)return true;
                    }
                }
            }
        }
    
        return false;
    }
    
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--){
            read_gragh();
            if(spfa())puts("possible");
            else puts("not possible");
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    sqlalchemy 使用pymysql连接mysql 1366错误
    mysql之数据导出
    Go常见语句
    huffman code
    后缀数组,目前比较赶进度,而且有点难,所以放到以后再来看
    hash
    bipartite matching
    spanning tree
    拓扑排序
    Union Find
  • 原文地址:https://www.cnblogs.com/sbfhy/p/5914317.html
Copyright © 2011-2022 走看看