博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3259 Wormholes 最短路bellman 题意转化很重要
阅读量:4112 次
发布时间:2019-05-25

本文共 3129 字,大约阅读时间需要 10 分钟。

Wormholes
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 38920 Accepted: 14307

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! Each of FJ's farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..NM (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.

As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .

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 integer, FF farm descriptions follow. 
Line 1 of each farm: Three space-separated integers respectively: NM, and W 
Lines 2..M+1 of each farm: Three space-separated numbers (SET) that describe, respectively: a bidirectional path between S and Ethat requires T seconds to traverse. Two fields might be connected by more than one path. 
Lines M+2..M+W+1 of each farm: Three space-separated numbers (SET) that describe, respectively: A one way path from S to Ethat also moves the traveler back T seconds.

Output

Lines 1..F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).

Sample Input

23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8

Sample Output

NOYES

Hint

For farm 1, FJ cannot travel back in time. 
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.

Source

错因:没有好好的分析和转化题意啊,,看到是从某些节点开始走,于是就想到了暴力枚举起点,就将问题转换成了一个多源点问题,结果写的非常复杂
乱成一团
解答:能从一个起点经过若干条路径回到起点后就能在沿途经过的时间总和上小于0,只需要在整个图中存在一个负环就好,只要起点在这个环上就可以了,而与起点具体在哪一点并无关系,这点理解非常关键,于是问题就转换成了一个简单的判断是否存在负环的。。。
#include
#include
#include
#include
#include
#include
using namespace std; #define inf 0x3f3f3f3f struct Edge{ int f,t,c; }edge[6000]; int dist[505]; int cas,n,m,h,cnt; int bellman() { fill(dist,dist+503+1,inf); dist[1]=0;int j; for(j=1;j<=n;j++) { bool flag=false; for(int i=1;i<=cnt;i++) { int from=edge[i].f,to=edge[i].t,cost=edge[i].c; if(dist[to]>dist[from]+cost) { flag=true; dist[to]=dist[from]+cost; } } if(!flag) break; } if(j==n+1) return 1; else return 0; } int main() { scanf("%d",&cas); while(cas--) { cnt=0; scanf("%d %d %d",&n,&m,&h); int u,v,w; for(int i=1;i<=m;i++) { cnt++; scanf("%d %d %d",&u,&v,&w); edge[cnt].f=u; edge[cnt].t=v; edge[cnt].c=w; cnt++; edge[cnt].f=v; edge[cnt].t=u; edge[cnt].c=w; } for(int i=1;i<=h;i++) { cnt++; scanf("%d %d %d",&u,&v,&w); edge[cnt].f=u; edge[cnt].t=v; edge[cnt].c=-w; } if(bellman()) printf("YES\n"); else printf("NO\n"); } return 0; }

转载地址:http://zvgsi.baihongyu.com/

你可能感兴趣的文章
去哪儿一面+平安科技二面+hr面+贝贝一面+二面产品面经
查看>>
element ui 弹窗在IE11中关闭时闪现问题修复
查看>>
vue 遍历对象并动态绑定在下拉列表中
查看>>
Vue动态生成el-checkbox点击无法选中的解决方法
查看>>
python __future__
查看>>
MySQL Tricks1
查看>>
python 变量作用域问题(经典坑)
查看>>
pytorch
查看>>
pytorch(二)
查看>>
pytorch(三)
查看>>
pytorch(四)
查看>>
pytorch(5)
查看>>
pytorch(6)
查看>>
ubuntu相关
查看>>
C++ 调用json
查看>>
nano中设置脚本开机自启动
查看>>
动态库调动态库
查看>>
Kubernetes集群搭建之CNI-Flanneld部署篇
查看>>
k8s web终端连接工具
查看>>
手绘VS码绘(一):静态图绘制(码绘使用P5.js)
查看>>