题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是记录这个点松弛进队的次数,次数超过点的个数的话,就说明存在负环使其不断松弛. #include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace st…
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 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…
Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36425   Accepted: 13320 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…
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…
原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退时间,问你这个农民可不可以回到起点,并且在他出发的时间之前. 题解 就虫洞用负边链接,然后判断是否有负环即可. 代码 #include<iostream> #include<cstring> #include<vector> #include<string> #…
题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个时间点之前,也就是时光倒流了 C 个时间并且此时你在 B 点,现在问你是否能够在图上的这些点中走,使得在某一个点刚好碰到之前的自己 分析 : 冷静分析一下,只要是有负权回路的某一条边属于虫洞的,那么肯定是能在这个环上一直绕,直到遇到之前的自己,如果将虫洞看作一条负权边的话,那么问题就变成了只要存在负…
ballman_ford 是对单源点到任意点最短路的处理方法(可以含负权边). 对所有边进行n-1次循环,(n为点得个数),如果此时源点到这条边终点的距离 大于 源点到这条边起点的距离加上路得权值就进行更新. 题目链接 题意:FJ农场主有F个农场, 在每个农场内有 N个点, M条双向路, W个单向虫洞, 每条路需要消耗一定的时间, 每个虫洞可以使得自己回到几秒前, 现在问FJ农场主可不可以遇到以前的自己. 题解: ballman_ford 判断负环, 存在负环就说明可以, 不存在就不可以. #i…
http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i],对应的边权为e[i],  即要求:∑(i=1,n)v[i]/∑(i=1,n)e[i]最大的环(n为环的点数),  设题目答案为ans,  即对于所有的环都有 ∑(i=1,n)(v[i])/∑(i=1,n)(e[i])<=ans  变形得ans* ∑(i=1,n)(e[i])>=∑(i=1,n)(v…
题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环.  两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边.” 如果一个结点经过了两次,那么我们走了一个圈.如果这个圈的权为正,显然不划算:如果是负圈,那么最短路不存在:如果是零圈,去掉不影响最优值. 也就是说,每个点最多入队$n-1$次,可以想象一下,左边$n-1$个节点全部指向右边一个节点,遍历的顺序恰好与边权顺序相反. 负圈是指圈上的总和小于0 实际只…
题意: 给一个混合图,求判断是否有负环的存在,若有,输出YES,否则NO.有重边. 思路: 这是spfa的功能范围.一个点入队列超过n次就是有负环了.因为是混合图,所以当你跑一次spfa时发现没有负环,但是负环仍可能存在,因为有向边! 但是单源最短路也有起点啊,难道穷举起点?不用,负环是必须有某些边是带负权的,那么我们只要穷举负权边的起点就行了,因为单单跑一次spfa不能保证能遍历所有点,但是如果穷举负权边起点还没有找到负环,那么负环不可能存在(剩下的都是正权,怎么可能有负环). //#incl…
POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 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 destin…
Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3259 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar b…
/** problem: http://poj.org/problem?id=3259 spfa判负环: 当有个点被松弛了n次,则这个点必定为负环中的一个点(n为点的个数) spfa双端队列优化: 维护队列使其dist小的点优先处理 **/ #include<stdio.h> #include<deque> #include<algorithm> using namespace std; class Graphics{ ; * + ; const static int…
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 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…
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 44090   Accepted: 16203 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is ver…
 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…
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…
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…
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions:75598   Accepted: 28136 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very…
poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根据套路,二分是显然的.然后跑一下spfa判断正环就行了. 然而我被no solution坑了十次提交.. #include <cctype> #include <cstdio> #include <cstring> using namespace std; const in…
Currency Exchange 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/E Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operati…
<题目链接> 题目大意: John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己.总的来说,就是看图中有没有负权环. 解题分析:判负环模板题,下面用的是spfa算法.判负环的依据为:如果在最短路的松弛操作中,存在进入队列次数大于n的点,则说明该图存在负环. #include <cstdio> #include <cstring&g…
King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14791   Accepted: 5226 Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ``If my child was a son and if only he was a sound kin…
King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11946   Accepted: 4365 Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ``If my child was a son and if only he was a sound kin…
tag是假的,用了及其诡异的方法判负环 正权无向边和负权有向边的图 #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=505,inf=210000000; int n,m,w,h[N],cnt,d[N]; struct qwe { int ne,to,va; }e[N*N]; int read() { int r=0,f=1; char p=get…
Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N (从1..N标号)块地,并有W个虫洞.其中1<=N<=500,1<=M<=2500,1<=W<=200. 现在John想借助这些虫洞来回到过去(出发时刻之前),请你告诉他能办到吗. John将向你提供F(1<=F<=5)个农场的地图.没有小路会耗费你超过100…
题目连接:http://poj.org/problem?id=3259 题意:John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己.总的来说,就是看图中有没有负权环.有的话就是可以,没有的话就是不可以了. 分析:sfa判负环,直接建图套模板即可. #include <cstdio> #include <cstring> #includ…
题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下去) 注意:双方向连通要把边起点终点互换后的边加上 */ #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstring> #…
http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W个 虫洞组成,FJ想从一块土地开始,经过若干条路和虫洞,返回到他最初开始走的地方并且时间要在他离开之前,或者恰好等于他离开的时间. 把虫洞的时间看成负边权,就是判断从起点出发是否存在负权回路.那么就可以采用bellman-ford算法,注意数组开大点. /* ********************…
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…