To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds. Input Line 1: A single...
3259:几乎相同的SPFA判负环,这里写了DFS(判环比BFS的快) CODE #include<cstdio>#include<cstring>usingnamespacestd;constintN=10005;structdata{intto,next,v;}e[N];intt,n,m,w,i,x,y,z,k,dis[N],vis[N],head[N];boolflag;inlinevoidread(int&x){x=0;charch=getchar();while(ch<'0'||ch...
LinesM+2..M+W+1 of each farm: Three space-separated numbers (S,E,T) that describe, respectively: A one way path fromStoEthat also moves the traveler backTseconds. Output Lines 1..F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include ...
http://poj.org/problem?id=3259 Sample Input 2 3 3 1 1 2 2 1 3 4 2 3 1 3 1 3 3 2 1 1 2 3 2 3 4 3 1 8 Sample Output NO YES 题目:输入农田个数,连通的路数,虫洞的个数。两农田之间连通是双向的,而虫洞是单向的。虫洞有这样的性质:可是时间倒流。问你这个农民能不能看到他自己,...
/*最短路 Bellman_Ford 求是否存在负权回路*/#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#defineN 6000#defineINF 0x7fffffffusingnamespacestd; typedefstruct{intu,v,w; } by; by b[N];intn,m,w;intBellman_Ford(intf) ...
#include#include#include#include#includeusing namespace std;#define INF 100000000int u[6000],v[6000],w[6000];int first[6000],next[6000];int coun[6000]
本题可抽象为:给出一些点和边,边分为两种,一种为双向边,权值为正,一种为单向边,权值为负。要求给定一个图,判断图中是否有负环。很显然,需要Bellman-Ford算法。因为本题只需判断是否有负环的存在,而不需求最短路,所以可令初始dis均为0,如果第n次松弛成功,则有负环;否则没有。
这里写一种较快的,dfs版的spfa。详见代码。AC代码:#include<cstdio> #include<cstring> #include<algorithm> #define R register using namespace std; inline int read(){ R int x=0;bool f=1; R char ch=getchar(); while(ch<'0'||ch>'...
~HDMaxfun 粉丝- 13 关注- 14 +加关注 1 升级成为会员 « 上一篇: ACM: Gym 100935B Weird Cryptography - 简单的字符串处理 » 下一篇: ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪 posted @ 2016-08-11 22:28 ~HDMaxfun 阅读(391) 评论(0) 编辑 收藏 举报 刷新...