poj 3259 Wormholes(spfa)

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1024;

struct node
{
    int to;
    int w;
    node *next;
};

node* edge[N];
int n,m,w,cnt[N],vis[N],dist[N];
queue<int>q;

int spfa(int v0)
{
    while(!q.empty()) q.pop();
    int i,u;
    node *ptr;
    for(i=1; i<N; i++)
    {
        dist[i]=inf;
        vis[i]=0;
        cnt[i]=0;
    }
    dist[v0]=0;
    vis[v0]=1;
    q.push(v0);
    cnt[v0]++;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        vis[u]=0;
        if(cnt[u]>n) return 1;
        ptr=edge[u];
        while(ptr!=NULL)
        {
            if(dist[ptr->to]>dist[u]+ptr->w)
            {
                dist[ptr->to]=dist[u]+ptr->w;
                if(vis[ptr->to]==0)
                {
                    vis[ptr->to]=1;
                    cnt[ptr->to]++;
                    q.push(ptr->to);
                }
            }
            ptr=ptr->next;
        }
    }
    return 0;
}

int main()
{
    int _,i,u,v,t,flag;
    node* temp;
    scanf("%d",&_);
    while(_--)
    {
        scanf("%d%d%d",&n,&m,&w);
        for(i=1; i<N; i++)
            edge[i]=NULL;

        for(i=0; i<m; i++)
        {
            scanf("%d%d%d",&u,&v,&t);
            temp=new node;
            temp->to=v;
            temp->w=t;
            temp->next=NULL;
            if(edge[u]==NULL)
            {
                edge[u]=temp;
            }
            else
            {
                temp->next=edge[u];
                edge[u]=temp;
            }

            temp=new node;
            temp->to=u;
            temp->w=t;
            temp->next=NULL;
            if(edge[v]==NULL)
            {
                edge[v]=temp;
            }
            else
            {
                temp->next=edge[v];
                edge[v]=temp;
            }

        }
        for(i=0; i<w; i++)
        {
            scanf("%d%d%d",&u,&v,&t);
            temp=new node;
            temp->to=v;
            temp->w=-t;
            temp->next=NULL;
            if(edge[u]==NULL)
            {
                edge[u]=temp;
            }
            else
            {
                temp->next=edge[u];
                edge[u]=temp;
            }
        }
        flag=spfa(1);
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

时间: 2024-08-07 21:19:52

poj 3259 Wormholes(spfa)的相关文章

POJ 3259 Wormholes SPFA算法题解

本题其实也可以使用SPFA算法来求解的,不过就一个关键点,就是当某个顶点入列的次数超过所有顶点的总数的时候,就可以判断是有负环出现了. SPFA原来也是可以处理负环的. 不过SPFA这种处理负环的方法自然比一般的Bellman Ford算法要慢点了. #include <stdio.h> #include <string.h> #include <limits.h> const int MAX_N = 501; const int MAX_M = 2501; const

ACM: POJ 3259 Wormholes - SPFA负环判定

POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way pa

poj 3259 Wormholes spfa : 双端队列优化 判负环 O(k*E)

1 /** 2 problem: http://poj.org/problem?id=3259 3 spfa判负环: 4 当有个点被松弛了n次,则这个点必定为负环中的一个点(n为点的个数) 5 spfa双端队列优化: 6 维护队列使其dist小的点优先处理 7 **/ 8 #include<stdio.h> 9 #include<deque> 10 #include<algorithm> 11 using namespace std; 12 13 class Graph

POJ 3259 Wormholes(SPFA判负环)

题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是记录这个点松弛进队的次数,次数超过点的个数的话,就说明存在负环使其不断松弛. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using na

POJ 3259 Wormholes(SPFA)

Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Eac

POJ 3259 Wormholes (图论---最短路 Bellman-Ford || SPFA)

链接:http://poj.org/problem?id=3259 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BE

Poj 3259 Wormholes 负环判断 SPFA &amp; BellmanFord

#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <

[2016-04-03][POJ][3259][Wormholes]

时间:2016-04-03 20:22:04 星期日 题目编号:[2016-04-03][POJ][3259][Wormholes] 题目大意:某农场有n个节点,m条边(双向)和w个虫洞(单向),(走虫洞可以回到过去的时间),问能否和过去的自己相遇 分析: 题目意思就是给定一个带负权的图,问是否存在负圈, spfa_bfs spfa_dfs bellman_ford #include <vector> #include <queue> #include <algorithm&

poj 3259 Wormholes (Bellman-ford)

链接: poj 3259 题意:一个famer有一些农场,这些农场里面有一些田地,田地里面有一些虫洞,田地和田地之间有路(双向的),即从a到b和从b到a时间都为c.虫洞的性质:时间倒流.即通过虫洞从a到b所花时间为 -c(单向的).问从某块田出发,他能否通过虫洞的性质回到出发点前 思路:这题实际就是判断是否存在负权回路,可以用SPFA算法或Bellman-Ford算法判断.若存在负权回路,则可以达到目的,否则不可以. Bellman-ford算法 #include<stdio.h> #incl