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

题意:主要就是构造图, 然后判断,是否存在负图,可以回到原点

/*
2
3 3 1 //N, M, W 1 2 2
1 3 4
2 3 1 3 1 3 //虫洞 3 2 1 //N, M, W 1 2 3
2 3 4 3 1 8 */
#include <iostream>
#include <cstring>
using namespace std; const int maxn = ( + + ) * + ;
const int INF = + ;
int N, M, W; //(from, to) 权值为cost
struct Edge {
int from,
to, cost;
Edge(int f = , int t = , int c = ) :
from(f), to(t), cost(c) {}
}; //边
Edge es[maxn]; int d[maxn]; //最短距离
int V, E; //顶点数,E边数 bool find_negative_loop()
{
memset(d, , sizeof(d)); for (int i = ; i < V; i++)
{
for (int j = ; j < E; j++) {
Edge e = es[j];
if (d[e.to] > d[e.from] + e.cost) {
d[e.to] = d[e.from] + e.cost; //如果第n次仍然更新了,则存在负圈
if (i == V - ) return true;
}
}
}
return false;
} void solve()
{
int F;
int from, to, cost; scanf("%d", &F);
while (F--)
{
scanf("%d%d%d", &N, &M, &W); //顶点数,边数, 虫洞数
V = N; E = ; // E = M * 2 应该
for (int i = ; i < M; ++i)
{
cin >> from >> to >> cost;
--from; --to;
//无向图 -- 去
es[E].from = from; es[E].to = to;
es[E].cost = cost; ++E;
//回 -- 再来一次
es[E].from = to; es[E].to = from;
es[E].cost = cost; ++E;
} for (int i = ; i < W; i++)
{
cin >> from >> to >> cost;
--from; --to;
es[E].from = from;
es[E].to = to;
//虫洞 - 回路
es[E].cost = -cost;
++E;
}
if (find_negative_loop()) {
printf("YES\n");
} else {
printf("NO\n");
}
}
} int main()
{
solve(); return ; }

POJ No 3259 Wormholes Bellman-Ford 判断是否存在负图的更多相关文章

  1. uva 558 - Wormholes(Bellman Ford判断负环)

    题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...

  2. POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】

    链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  3. poj 3259 Wormholes(bellman-ford判断负环)

    题目链接:http://poj.org/problem?id=3259 题目就是问你能否回到原点而且时间还倒回去了.题目中有些路中有单向的虫洞能让时间回到过去 所以只要将虫洞这条边的权值赋为负然后再判 ...

  4. POJ 3259 Wormholes Bellman题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  5. poj 3259 Wormholes【spfa判断负环】

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36729   Accepted: 13444 Descr ...

  6. (简单) POJ 3259 Wormholes,SPFA判断负环。

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  7. POJ 3259 Wormholes【Bellman_ford判断负环】

    题意:给出n个点,m条正权的边,w条负权的边,问是否存在负环 因为Bellman_ford最多松弛n-1次, 因为从起点1终点n最多经过n-2个点,即最多松弛n-1次,如果第n次松弛还能成功的话,则说 ...

  8. 【POJ】3259 Wormholes

    题目链接:http://poj.org/problem?id=3259 题意:n个农场,m条双向路径,w条单向路径(虫洞).单向虫洞路径是负值.农夫想知道自己能不能看到自己(X). 题解:其实刚开始没 ...

  9. poj1860 兑换货币(bellman ford判断正环)

    传送门:点击打开链接 题目大意:一个城市有n种货币,m个货币交换点,你有v的钱,每个交换点只能交换两种货币,(A换B或者B换A),每一次交换都有独特的汇率和手续费,问你存不存在一种换法使原来的钱更多. ...

随机推荐

  1. HDU 1565 方格取数(1) 轮廓线dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1565 方格取数(1) Time Limit: 10000/5000 MS (Java/Others) ...

  2. Tensorflow踩坑之tf.nn.bidirectional_dynamic_rnn()报错 “ValueError: None values not supported.”

    详细解决方法见链接:https://stackoverflow.com/questions/39808336/tensorflow-bidirectional-dynamic-rnn-none-val ...

  3. ASP.NET MVC 4.0 参考源码索引

    http://www.projky.com/asp.netmvc/4.0/Microsoft/AspNet/Mvc/Facebook/FacebookAppSettingKeys.cs.htmlhtt ...

  4. 补交第一周:coding net

    coding net:https://coding.net/u/yuanyuancheng git openssh: 四则运算 https://git.coding.net/yuanyuancheng ...

  5. 远程修改VMware ESXi服务器的密码(SSH)

    1,用vSphere client登录到服务器,将SSH启用. 2,使用ssh连接工具(我用的是secureCRT)远程登录,输入passwd,键入两次新密码,OK.  

  6. lr关联-保存数组并调用(转)

    LOADRUNNER中的一个关联技巧     众所周知,在LoadRunner中,关联是一个很重要的动作,大多数的脚本在录制完成后并不能直接回放,需要通过一定的关联才能成功回放.关联的技巧有很多,这里 ...

  7. build.xml

    下载ant 解压ant 后设置ANT_HOME, PATH中添加ANT_HOME目录下的bin目录(如:ANT_HOME:,PATH:D:\apache-ant-1.9.2%ANT_HOME%\bin ...

  8. pygame学习笔记(3)——时间、事件、文字

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 1.运动速率    上节中,实现了一辆汽车在马路上由下到上行驶,并使用了pygame.time.delay(200 ...

  9. js & auto copy

    js & auto copy https://developer.mozilla.org/zh-CN/docs/Web/Events/copy Ctrl + C Command + C doc ...

  10. 过滤器将获取到的内容注入到servlet的request中

    过滤器将获取到的内容注入到servlet的request中