POJ3259 Wormholes 【spfa判负环】】的更多相关文章

题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环.  两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边.” 如果一个结点经过了两次,那么我们走了一个圈.如果这个圈的权为正,显然不划算:如果是负圈,那么最短路不存在:如果是零圈,去掉不影响最优值. 也就是说,每个点最多入队$n-1$次,可以想象一下,左边$n-1$个节点全部指向右边一个节点,遍历的顺序恰好与边权顺序相反. 负圈是指圈上的总和小于0 实际只…
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…
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 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是记录这个点松弛进队的次数,次数超过点的个数的话,就说明存在负环使其不断松弛. #include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace st…
题目连接:http://poj.org/problem?id=3259 题意:John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己.总的来说,就是看图中有没有负权环.有的话就是可以,没有的话就是不可以了. 分析:sfa判负环,直接建图套模板即可. #include <cstdio> #include <cstring> #includ…
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 55082   Accepted: 20543 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is ver…
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…
Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N (从1..N标号)块地,并有W个虫洞.其中1<=N<=500,1<=M<=2500,1<=W<=200. 现在John想借助这些虫洞来回到过去(出发时刻之前),请你告诉他能办到吗. John将向你提供F(1<=F<=5)个农场的地图.没有小路会耗费你超过100…
bfs版spfa void spfa(){ queue<int> q; ;i<=n;i++) dis[i]=inf; q.push();dis[]=;vis[]=; while(!q.empty()){ int u=q.front();q.pop(); for(int i=head[u];i;i=e[i].next){ int v=e[i].v,w=e[i].w; if(dis[v]>dis[u]+w){ dis[v]=dis[u]+w; if(!vis[v]){ vis[v]=;…
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…