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

题意:一个famer有一些农场,这些农场里面有一些田地,田地里面有一些虫洞,田地和田地之间有路,虫洞有这样的性质: 时间倒流。问你这个农民能不能看到他自己,也就是说,有没有这样一条路径,能利用虫洞的时间倒流的性质,让这个人能在这个点出发前回去,这样他就是能看到他自己

典型的Bellman_ford 检查有没有形成负环。

套的模板

 #include <stdio.h>
#include <string.h> const int maxn = ;
const int maxm = ;
const int oo = <<;
struct node{
int u;
int v;
int w;
int next;
}edge[maxm];
int dis[maxn];
int cnt;
int n, m; void add(int u, int v, int w){
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt++].w = w;
} bool bellman_ford(int s){
for(int i = ; i < n; i++){
dis[i] = oo;
}
dis[s] = ;
for(int i = ; i < n-; i++){
for(int j = ; j < cnt; j++){
int u = edge[j].u;
int v = edge[j].v;
int w = edge[j].w;
if(dis[v] > dis[u] + w){
dis[v] = dis[u] + w;
}
}
}
for(int j = ; j < cnt; j++){
int u = edge[j].u;
int v = edge[j].v;
int w = edge[j].w;
if(dis[v] > dis[u] + w){
return false;
}
}
return true;
} void init(){
cnt = ;
} int main()
{
int t,x;
scanf("%d",&t);
while(t--)
{
scanf("%d %d %d", &n, &m, &x);
int u, v, w;
init();
for(int i = ; i < m; i++){
scanf("%d %d %d", &u, &v, &w);
add(u, v, w);
add(v, u, w);
}
while(x--)
{
scanf("%d %d %d", &u, &v, &w);
add(u, v, -w);
}
if(bellman_ford()) printf("NO\n");
else printf("YES\n");
}
return ;
}

discuss里面还有spfa 的代码:http://poj.org/showmessage?message_id=162146

就是 又设了一个cnt数组来记录各个点用过的 次数,如果次数大于等于n说明有负环

poj 3259 Wormholes(最短路 Bellman)的更多相关文章

  1. POJ 3259 Wormholes 最短路+负环

    原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退 ...

  2. POJ 3259 Wormholes (最短路)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 34302   Accepted: 12520 Descr ...

  3. 最短路(Bellman_Ford) POJ 3259 Wormholes

    题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...

  4. poj - 3259 Wormholes (bellman-ford算法求最短路)

    http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...

  5. ACM: POJ 3259 Wormholes - SPFA负环判定

     POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

  6. POJ 3259 Wormholes(最短路径,求负环)

    POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...

  7. POJ 3259 Wormholes(最短路,判断有没有负环回路)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24249   Accepted: 8652 Descri ...

  8. POJ 3259——Wormholes——————【最短路、SPFA、判负环】

    Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit St ...

  9. POJ 3259 Wormholes (Bellman_ford算法)

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

随机推荐

  1. javassist动态修改class

    import java.io.IOException;import java.io.PrintWriter; import javassist.CannotCompileException;impor ...

  2. YII千万级PV架构经验分享--俯瞰篇--架构即产品

    hello,大家好,我是方少,今天想问大家一个问题,如果我们自己就是产品,那么我们怎样才能说服别人,我们是最优秀的呢?高学历,不错,别人成功过了.会php,java,c#,python不行再学c++, ...

  3. WPF 中 TreeListView 的使用

    前段时间在项目开发中需要用 TreeListView 的功能,于是在网上狂搜一通,倒也找到了几个小例子,但还是满足不了我简单的要求,由于时间紧也只能折中凑合着用了.最近时间比较充裕,把其中的例子整理一 ...

  4. HDU 1159

    Description A subsequence of a given sequence is the given sequence with some elements (possible non ...

  5. 几种解析xml方式的比较

    1: DOM DOM 是用与平台和语言无关的方式表示 XML 文档的官方 W3C 标准.DOM 是以层次结构组织的节点或信息片断的集合.这个层次结构允许开发人员在树中寻找特定信息.分析该结构通常需要加 ...

  6. EXPLAIN句法 优化表结构

    EXPLAIN tbl_name or EXPLAIN SELECT select_options EXPLAIN tbl_name是DESC[RIBE] tbl_name或SHOW COLUMNS ...

  7. Kinetic使用注意点--lable

    new Lable(config) 参数: config:包含所有配置项的对象. { x: "横坐标", y: "纵坐标", width: "宽度&q ...

  8. 使用pd设计表的 多对多的中间表的设计方式, 有图有真相

    设计多对多表时解决重复问题 目前流行两种设计方式: 方式一 是在中间表中建一个单独的id主键, 与业务表关联的键设置为unique唯一; 干事二: 联合主键的方式, 该方式中间表不会有与业务表无关的主 ...

  9. Matlab求极限

    matlab求极限(可用来验证度量函数或者隶属度函数)可用来验证是否收敛,取值范围等等. 一.问题来源 搜集聚类资料时,又看到了隶属度函数,没错,就是下面这个,期间作者提到m趋于2是,结果趋于1,我想 ...

  10. [转载]WCF序列化65536大小限制的问题

    错误: The formatter threw an exception while trying to deserialize the message: There was an error whi ...