【POJ-3259】 Wormholes(判负环,spfa算法)

题干:

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 E that requires Tseconds to traverse. Two fields might be connected by more than one path. 
Lines M+2.. MW+1 of each farm: Three space-separated numbers ( SET) that describe, respectively: A one way path from S to E that 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

2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8

Sample Output

NO
YES

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.

题目大意:

有若干个虫洞,给出了若干普通路径和其所用时间以及虫洞的路径和其倒回的时间,现问你能否回到出发之前的时间,注意普通路径是双向的,虫洞是单向的。

解题报告:

由题目所给信息已经可以构建一张完整的图了,然后进一步理解题目的意思其实是这张图是否存在负环,因此使用Bellman_Ford或者spfa即可。

AC代码:(邻接表储存图)(266ms)

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,cnt;
const int MAX = 505;
const int INF = 0x3f3f3f3f;
int dis[MAX],maze[MAX][MAX],cntt[MAX],head[MAX];
bool vis[MAX];
struct Edge {int to,w,ne;Edge(){}//没有此构造函数不能写  node t  这样Edge(int to,int w,int ne):to(to),w(w),ne(ne){}//可以写node(pos,cost)这样} e[200000 + 5];//数组别开小了 
void add(int u,int v,int w) {e[cnt].to = v;e[cnt].w = w;e[cnt].ne = head[u];head[u] = cnt++;
}
bool spfa(int s){dis[s]=0; vis[s]=1;  //队列初始化,s为起点int v;queue<int > q;q.push(s);while (!q.empty()){   //队列非空v=q.front();  //取队首元素q.pop();vis[v]=0;   //释放队首结点,因为这节点可能下次用来松弛其它节点,重新入队for(int i=head[v]; i!=-1; i=e[i].ne)  //对所有顶点{if (dis[e[i].to]>dis[v]+e[i].w) {  dis[e[i].to] = dis[v]+e[i].w;   //修改最短路if (vis[e[i].to]==0) {  //如果扩展结点i不在队列中,入队cntt[e[i].to]++;vis[e[i].to]=1;q.push(e[i].to);if(cntt[e[i].to] >=n) return true;}}}}return false;
}void init() {cnt = 0;memset(vis,0,sizeof(vis));memset(maze,0,sizeof(maze));memset(e,0,sizeof(e));memset(dis,INF,sizeof(dis));memset(cntt,0,sizeof(cntt));memset(head,-1,sizeof(head)); 
}
int main()
{int t;int M,W,u,v,w;cin>>t;	while(t--) {init();scanf("%d%d%d",&n,&M,&W);for(int i = 1; i<=M; i++) {scanf("%d%d%d",&u,&v,&w);add(u,v,w);add(v,u,w);}for(int i = 1; i<=W; i++) {scanf("%d%d%d",&u,&v,&w);add(u,v,-w);}cntt[1] =1;if(spfa(1)) printf("YES\n");else printf("NO\n");}return 0 ;
}

AC代码2:(邻接矩阵)(需要判重边!)(1079ms)

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n;
const int MAX = 505;
const int INF = 0x3f3f3f3f;
int dis[MAX],maze[MAX][MAX],cnt[MAX];
bool vis[MAX];
bool spfa(int s){
//	for(int i=0; i<=n; i++) dis[i]=99999999; //初始化每点i到s的距离dis[s]=0; vis[s]=1;  //队列初始化,s为起点int i, v;queue<int > q;q.push(s);while (!q.empty()){   //队列非空v=q.front();  //取队首元素q.pop();vis[v]=0;   //释放队首结点,因为这节点可能下次用来松弛其它节点,重新入队for(i=1; i<=n; i++)  //对所有顶点if (maze[v][i]!=INF && dis[i]>dis[v]+maze[v][i]){  dis[i] = dis[v]+maze[v][i];   //修改最短路if (vis[i]==0){  //如果扩展结点i不在队列中,入队cnt[i]++;vis[i]=1;q.push(i);if(cnt[i] >=n) return true;}}}return false;
}void init() {memset(vis,0,sizeof(vis));memset(maze,INF,sizeof(maze));memset(dis,INF,sizeof(dis));memset(cnt,0,sizeof(cnt));
}
int main()
{int t;int M,W,u,v,w;cin>>t;	while(t--) {init();scanf("%d%d%d",&n,&M,&W);for(int i = 1; i<=M; i++) {scanf("%d%d%d",&u,&v,&w);if(w<maze[u][v])maze[u][v] = maze[v][u] = w;}for(int i = 1; i<=W; i++) {scanf("%d%d%d",&u,&v,&w);maze[u][v] = -w;}cnt[1] =1;if(spfa(1)) printf("YES\n");else printf("NO\n");}return 0 ;
}

 AC代码3:Bellman_Ford算法(125ms)

//Bellman_Ford算法试试
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 3000*2
#define INF 0xFFFFFFFint t , n , m, w;
int dis[MAXN];
struct Edge{int x;int y;int value;
}e[MAXN];bool judge(){for(int i = 0 ; i < m*2+w ; i++){if(dis[e[i].y] > dis[e[i].x] + e[i].value)return false;}return true;
}void Bellman_Ford(){dis[1] = 0;for(int i = 2 ; i <= n ; i++)dis[i] = INF;for(int i = 1 ; i <= n ; i++){for(int j = 0 ; j < m*2+w; j++){if(dis[e[j].y] > dis[e[j].x] + e[j].value)dis[e[j].y] = dis[e[j].x] + e[j].value;}}if(judge())printf("NO\n");elseprintf("YES\n");
}int main()
{int uu,vv,ww,i;scanf("%d",&t);while(t--){scanf("%d%d%d",&n,&m,&w);for(i=0;i<m*2;){scanf("%d%d%d",&uu,&vv,&ww);e[i].x=uu;e[i].y=vv;e[i++].value=ww;e[i].x=vv;e[i].y=uu;e[i++].value=ww;}for(;i<m*2+w;i++){scanf("%d%d%d",&uu,&vv,&ww);e[i].x=uu;e[i].y=vv;e[i].value=-ww;}Bellman_Ford();}return 0;
}

总结:

    1.脑残了吧调了一下午+一晚上bug尝试了邻接矩阵邻接表,发现是写了memset(dis,INF,sizeof(INF));这一行乐色?呵呵

    2.如果用邻接表储存这题别忘了去重一下不然会wa的,至于为什么权值为负的时候不需要判重,大概是因为这题只要是负的,都不影响结果?因为这题要看的是,是否存在负环啊。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441964.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【HihoCoder - 1550】顺序三元组(思维)

题干&#xff1a; 给定一个长度为N的数组A[A1, A2, ... AN]&#xff0c;已知其中每个元素Ai的值都只可能是1, 2或者3。 请求出有多少下标三元组(i, j, k)满足1 ≤ i < j < k ≤ N且Ai < Aj < Ak。 Input 第一行包含一个整数N 第二行包含N个整数A1, A2, ... …

joptionpane java_Java JOptionPane

Java JOptionPane1 Java JOptionPane的介绍JOptionPane类用于提供标准对话框&#xff0c;例如消息对话框&#xff0c;确认对话框和输入对话框。这些对话框用于显示信息或从用户那里获取输入。JOptionPane类继承了JComponent类。2 Java JOptionPane的声明public class JOptionPa…

【POJ - 3268 】Silver Cow Party(Dijkstra最短路+思维)

题干&#xff1a; One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; roa…

【HDU - 3342】Legal or Not(拓扑排序)

题干&#xff1a; ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas.…

java 股票 代码_Java中利用散列表实现股票行情的查询_java

---- 在java中&#xff0c;提供了一个散列表类Hashtable&#xff0c;利用该类&#xff0c;我们可以按照特定的方式来存储数据&#xff0c;从而达到快速检索的目的。本文以查询股票的收盘数据为例&#xff0c;详细地说明java中散列表的使用方法。一、散列表的原理---- 散列表&am…

deepin部署python开发环境_deepin系统下部署Python3.5的开发及运行环境

deepin系统下部署Python3.5的开发及运行环境1 概述本人小白一枚&#xff0c;由于最近要学习python接口自动化测试&#xff0c;所以记录一下相关学习经过及经验&#xff0c;希望对跟我一样小白的朋友可以有所帮助。2 下载在python官网下载指定平台下的python3.5的环境wget https…

【HDU - 2066】:一个人的旅行(Dijkstra算法)

题干&#xff1a; 虽然草儿是个路痴&#xff08;就是在杭电待了一年多&#xff0c;居然还会在校园里迷路的人&#xff0c;汗~),但是草儿仍然很喜欢旅行&#xff0c;因为在旅途中 会遇见很多人&#xff08;白马王子&#xff0c;^0^&#xff09;&#xff0c;很多事&#xff0c;还…

for相关 java_Java学习之for循环相关知识梳理

for循环是编程语言中一种循环语句&#xff0c;是Java程序员日常工作中的重要组成部分。循环语句由循环体及循环的判定条件两部分组成&#xff0c;其表达式为&#xff1a;for(单次表达式;条件表达式;末尾循环体){中间循环体&#xff1b;}。拉勾IT课小编为大家分析如何使用这一属…

【HDU - 3790】最短路径问题(DIjkstra算法 双权值)

题干&#xff1a; 给你n个点&#xff0c;m条无向边&#xff0c;每条边都有长度d和花费p&#xff0c;给你起点s终点t&#xff0c;要求输出起点到终点的最短距离及其花费&#xff0c;如果最短距离有多条路线&#xff0c;则输出花费最少的。 Input 输入n,m&#xff0c;点的编号…

【POJ - 3037】Skiing (Dijkstra算法)

题干&#xff1a; Bessie and the rest of Farmer Johns cows are taking a trip this winter to go skiing. One day Bessie finds herself at the top left corner of an R (1 < R < 100) by C (1 < C < 100) grid of elevations E (-25 < E < 25). In or…

java web权限设计数据权限范围_JavaWeb 角色权限控制——数据库设计

相信各位读者对于角色权限管理这个需求并不陌生。那么是怎么实现的呢&#xff1f;今天小编来说道说道&#xff01;1、首先我们来进行数据库的设计&#xff0c;如何设计数据库是实现权限控制的关键&#xff1a;1)用户表&#xff1a;id&#xff1a;主键、自增、intname&#xff1…

modbus与硬件对接Java_java中modbus协议连接

modbus在java中的使用&#xff0c;首先maven的pom中引入modbus4j包com.infiniteautomationmodbus4j3.0.32. 我们创建类&#xff1a;ModBus4JTCPClient&#xff0c;创建ModbusMaster连接对象&#xff0c;以及读取寄存器方法package io.powerx.test;import org.apache.commons.la…

【51Nod-1100】 斜率最大(贪心)☆双排序

题干&#xff1a; 平面上有N个点&#xff0c;任意2个点确定一条直线&#xff0c;求出所有这些直线中&#xff0c;斜率最大的那条直线所通过的两个点。 &#xff08;点的编号为1-N&#xff0c;如果有多条直线斜率相等&#xff0c;则输出所有结果&#xff0c;按照点的X轴坐标排…

【HDU - 3714 】Error Curves (三分)

题干&#xff1a; Josephina is a clever girl and addicted to Machine Learning recently. She pays much attention to a method called Linear Discriminant Analysis, which has many interesting properties. In order to test the algorithms efficiency, she colle…

java中JLabel添加监听事件_[求助]关于JLabel添加监听器的问题。请各位帮忙!!

[求助]关于JLabel添加监听器的问题。请各位帮忙&#xff01;&#xff01;如图&#xff0c;我想在左边的JLabel上添加事件监听器&#xff0c;然后再去右边的JPane上进行绘制图形&#xff0c;请问这个事件监听器改怎么加&#xff0c;好象不能加ActionListener&#xff0c;要加什么…

指数循环节证明

还有关键的一步忘写了phi(m)>r的注意因为ma^r*m‘’所以phi(m)>phi(a^r)>r,所以就相当于phi(m)为循环节&#xff0c;不过如果指数小于phi(m)只能直接算了。。 注意这里的m与a^r是互质的上面忘写了。。 转自https://blog.csdn.net/guoshiyuan484/article/details/787…

java语言中的类可以_java 语言中的类

类一、类类是具有相同性质的一类事物的总称, 它是一个抽象的概念。它封装了一类对象的状态和方法, 是创建对象的模板。类的实现包括两部分: 类声明和类体类的声明类声明的基本格式为:[ 访问权限修饰符]c l a s s类名[extends超类][ implments实现的接口列表]{}说 明:① 访问权限…

【HDU - 1546】 Idiomatic Phrases Game(Dijkstra,可选map处理字符串)

题干&#xff1a; Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters and has a certain meaning. This game will give Tom two idioms. He should build a list of idioms and the list starts and ends with the two…

Java迭代器修改链表_Java恼人的迭代器不会返回链表中的元素

给出以下代码&#xff1a;public void insertIntoQueue(float length,int xElement,int yElement,int whichElement){Dot dot new Dot(xElement,yElement);GeometricElement element null;// some codeint robotX,robotY;boolean flag false;for (Iterator i robotList.ite…

(精)【ACM刷题之路】POJ题目详细多角度分类及推荐题目

POJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094) 初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(poj…