POJ——T3259 Wormholes

http://poj.org/problem?id=3259

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 50692   Accepted: 18750

发现虫洞的的JOhn~开始他的神秘之~~~~

就是给你m条带权双向边,w条带权单向边

可以将单边制成负的,SFPA求一下是否有负环

 1 #include <cstring>
 2 #include <cstdio>
 3
 4 #define MIN(a,b) ( a<b ?a :b)
 5
 6 using namespace std;
 7
 8 const int N(2521);
 9 int t,n,m,c,u,v,w;
10 int head[N],sumedge;
11 struct Edge
12 {
13     int to,next,cost;
14     Edge(int to=0,int next=0,int cost=0):
15         to(to),next(next),cost(cost){}
16 }edge[N<<1];
17
18 void ins(int from,int to,int cost)
19 {
20     edge[++sumedge]=Edge(to,head[from],cost);
21     head[from]=sumedge;
22 }
23
24 int if_YES,vis[N],dis[N];
25
26 void SPFA(int now)
27 {
28     vis[now]=1;
29     if(if_YES) return ;
30     for(int i=head[now];i;i=edge[i].next)
31     {
32         int go=edge[i].to;
33         if(dis[go]>dis[now]+edge[i].cost)
34         {
35             if(vis[go])
36             {
37                 if_YES=1;
38                 break;
39             }
40             dis[go]=dis[now]+edge[i].cost;
41             SPFA(go);
42         }
43     }
44     vis[now]=0;
45 }
46
47 void init()
48 {
49     if_YES=sumedge=0;
50     memset(vis,0,sizeof(vis));
51     memset(dis,0,sizeof(dis));
52     memset(head,0,sizeof(head));
53 }
54
55 int main()
56 {
57     scanf("%d",&t);
58     for(;t;t--)
59     {    init();
60         scanf("%d%d%d",&n,&m,&c);
61         for(;m;m--)
62         {
63             scanf("%d%d%d",&u,&v,&w);
64             ins(u,v,w); ins(v,u,w);
65         }
66         for(;c;c--)
67         {
68             scanf("%d%d%d",&u,&v,&w);
69             ins(u,v,-w);
70         }
71         for(int i=1;i<=n;i++)
72         {
73             SPFA(i);
74             if(if_YES) break;
75         }
76         if(if_YES) printf("YES\n");
77         else        printf("NO\n");
78     }
79     return 0;
80 }
时间: 2024-10-07 16:17:35

POJ——T3259 Wormholes的相关文章

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

[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(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

[ACM] POJ 3259 Wormholes (bellman-ford最短路径,判断是否存在负权回路)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29971   Accepted: 10844 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 p

POJ 3259 Wormholes Bellman题解

本题就是需要检查有没有负环存在于路径中,使用Bellman Ford算法可以检查是否有负环存在. 算法很简单,就是在Bellman Ford后面增加一个循环判断就可以了. 题目故事很奇怪,小心读题. #include <stdio.h> #include <string.h> #include <limits.h> const int MAX_N = 501; const int MAX_M = 2501; const int MAX_W = 201; struct E

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 <

poj 3259 Wormholes(Bellman-Ford)

poj 3259 Wormholes 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 entere