题意:给你m条路花费时间(双向正权路径),w个虫洞返回时间(单向负权路径),问你他能不能走一圈回到原点之后,时间倒流。

思路:题意有点难看懂,我们建完边之后找一下是否存在负权回路,存在则能,反之不能。判断负权回路可以用一个cnt,这个spfa板子里有。

代码:

#include<cstdio>
#include<set>
#include<vector>
#include<cmath>
#include<queue>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = 500+5;
const int INF = 0x3f3f3f3f;
struct Edge{
int v;
int w;
Edge(int _v = 0,int _w = 0):v(_v),w(_w){}
};
vector<Edge> G[maxn];
bool vis[maxn];
int cnt[maxn];
int dis[maxn];
bool spfa(int start,int n){
memset(vis,false,sizeof(vis));
for(int i = 1;i <= n;i++) dis[i] = INF;
vis[start] = true;
dis[start] = 0;
queue<int> q;
while(!q.empty()) q.pop();
q.push(start);
memset(cnt,0,sizeof(cnt));
cnt[start] = 1;
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int i = 0;i < G[u].size();i++){
int v = G[u][i].v;
int w = G[u][i].w;
if(dis[v] > dis[u] + w){
dis[v] = dis[u] + w;
if(!vis[v]){
q.push(v);
vis[v] = true;
if(++cnt[v] > n)
return true;
}
}
}
}
return false;
}
void addEdge(int u,int v,int w){
G[u].push_back(Edge(v,w));
}
int main(){
int n,m,w;
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&w);
for(int i = 1;i <= n;i++) G[i].clear();
int s,e,t;
while(m--){
scanf("%d%d%d",&s,&e,&t);
addEdge(s,e,t);
addEdge(e,s,t);
}
while(w--){
scanf("%d%d%d",&s,&e,&t);
addEdge(s,e,-t);
}
if(spfa(1,n)){
printf("YES\n");
}
else{
printf("NO\n");
}
}
return 0;
}

POJ 3259 Wormholes(最短路&spfa正权回路)题解的更多相关文章

  1. POJ 1860 Currency Exchange(最短路&spfa正权回路)题解

    题意:n种钱,m种汇率转换,若ab汇率p,手续费q,则b=(a-q)*p,你有第s种钱v数量,问你能不能通过转化让你的s种钱变多? 思路:因为过程中可能有负权值,用spfa.求是否有正权回路,dis[ ...

  2. HDU - 1317 ~ SPFA正权回路的判断

    题意:有最多一百个房间,房间之间连通,到达另一个房间会消耗能量值或者增加能量值,求是否能从一号房间到达n号房间. 看数据,有定5个房间,下面有5行,第 iii 行代表 iii 号 房间的信息,第一个数 ...

  3. poj 3259 Wormholes(最短路 Bellman)

    题目:http://poj.org/problem?id=3259 题意:一个famer有一些农场,这些农场里面有一些田地,田地里面有一些虫洞,田地和田地之间有路,虫洞有这样的性质: 时间倒流.问你这 ...

  4. POJ 3259 Wormholes 最短路+负环

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

  5. 解题报告:poj 3259 Wormholes(入门spfa判断负环)

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

  6. POJ 3259 Wormholes (最短路)

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

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

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

  8. 最短路(Bellman_Ford) POJ 3259 Wormholes

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

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

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

随机推荐

  1. Web和Native使用外部字体ttf方法

    论坛教程:http://bbs.egret.com/forum.php?mod=viewthread&tid=24879&extra=&highlight=%E5%AD%97% ...

  2. [分享]收集的Linux学习资源

    下面是我收集的一些Linux资源,与大家分享.大家共同学习,一起进步. 国内的专业Linux网站(GB) 1. ChinaUnix:http://www.chinaunix.net/ 2. Linux ...

  3. go练习3 --map的操作

    func T2_1() {     // 键值string , 值 int 类型     m1 := map[string]int{}     //添加一个元素     m1["str1&q ...

  4. shell脚本抓取网页信息

    利用shell脚本分析网站数据 # define url time=$(date +%F) mtime=$(date +%T) file=/abc/shell/abc/abc_$time.log ht ...

  5. Python - 3.6 学习第一天

    开始之前 基础示例 Python语法基础,python语法比较简单,采用缩紧方式. # print absolute value of a integer a = 100 if a >= 0: ...

  6. 熵 互信息 Gini指数 和 不纯度

    在学习决策树类的算法时,总是绕不开 信息熵.Gini指数和它们相关联的概念,概念不清楚,就很难理解决策树的构造过程,现在把这些概念捋一捋. 信息熵 信息熵,简称熵,用来衡量随机变量的不确定性大小,熵越 ...

  7. javascript飞机大战-----008积分

    /* 创建敌机: */ function Enemy(blood,speed,imgs,scroe){ //敌机left this.left = 0; //敌机top this.top = 0; // ...

  8. oracle goldengate技术架构-简单试验(全)

    一  GoldenGate简介 Oracle Golden Gate软件是一种基于日志的结构化数据复制备份软件,它通过解析源 数据库在线日志或归档日志获得数据的增量变化,再将这些变化应用到目标数据库, ...

  9. Wireshark分析之TCP协议(二)

    (1)TCP首部格式 源端口:   用来传输数据报的端口 目标端口: 数据包将要发送到的端口 序号: 用来表示一个TCP片段.这个值用来表示数据流中的部分数据没有丢失 确认号:  表示通信中希望从另一 ...

  10. timedatectl — Control the system time and date

    timedatectl --help 的执行结果如下: timedatectl [OPTIONS...] COMMAND ... Query or change system time and dat ...