【POJ - 3259 】Wormholes(Bellman_Ford或spfa算法,判断有向图中是否存在负环)

题干:

农夫约翰在探索他的许多农场,发现了一些惊人的虫洞。虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径,W(1≤W≤200)个虫洞。FJ作为一个狂热的时间旅行的爱好者,他要做到以下几点:开始在一个区域,通过一些路径和虫洞旅行,他要回到最开时出发的那个区域出发前的时间。也许他就能遇到自己了:)。为了帮助FJ找出这是否是可以或不可以,他会为你提供F个农场的完整的映射到(1≤F≤5)。所有的路径所花时间都不大于10000秒,所有的虫洞都不大于万秒的时间回溯。

Input

第1行:一个整数F表示接下来会有F个农场说明。 每个农场第一行:分别是三个空格隔开的整数:N,M和W 第2行到M+1行:三个空格分开的数字(S,E,T)描述,分别为:需要T秒走过S和E之间的双向路径。两个区域可能由一个以上的路径来连接。 第M +2到M+ W+1行:三个空格分开的数字(S,E,T)描述虫洞,描述单向路径,S到E且回溯T秒。

Output

F行,每行代表一个农场 每个农场单独的一行,” YES”表示能满足要求,”NO”表示不能满足要求。

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.

 

AC代码:(spfa,邻接矩阵存图)(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代码2:(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;
}

 AC代码3:(spfa,邻接表储存图)(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代码4:(Bellmanford,加优化)(47ms)

//bellman算法:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 20100using namespace std;
const int INF = 0x3f3f3f3f;
int n,m,mm;
int tot;
int dis[6005];
struct Edge {int u,v,w;
} e[6005];
void add(int u,int v,int w) {e[++tot].u = u;e[tot].v = v;e[tot].w = w;
}
bool bell() {memset(dis,INF,sizeof(dis));dis[1]=0;int all = n;int flag = 0;while(all--) {flag = 0;for(int i = 1; i<=tot; i++) {if(dis[e[i].v] > dis[e[i].u] + e[i].w) {flag = 1;dis[e[i].v] = dis[e[i].u] + e[i].w;}}if(flag == 0) return false;}flag = 0;for(int i = 1; i<=tot; i++) {if(dis[e[i].v] > dis[e[i].u] + e[i].w) return 1;}return 0;
}int main()
{int f,a,b,c;cin>>f;while(f--) {tot = 0;scanf("%d%d%d",&n,&m,&mm);for(int i = 1; i<=m; i++) {scanf("%d%d%d",&a,&b,&c) ;add(a,b,c);add(b,a,c);}for(int i = 1; i<=mm; i++) {scanf("%d%d%d",&a,&b,&c);add(a,b,-c);}if(bell()) puts("YES");else puts("NO");}return 0 ;
}
//2
//3 3 1
//1 2 2
//1 3 4
//2 3 1
//3 1 3

 

 

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

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

相关文章

android设置window背景颜色,android – 设置标题背景颜色

这个thread将让你开始在xml文件中建立自己的标题栏&#xff0c;并在你的活动中使用它编辑这里是上面链接的内容的简要摘要 – 这只是设置文本的颜色和标题栏的背景 – 没有调整大小&#xff0c;没有按钮&#xff0c;只是最简单的示例res / layout / mytitle.xml – 这是将表示标…

ios 代码设置控件宽高比_iOS--利用比例纯代码适配屏幕大小

首先说下让自己的程序支持iPhone6和6&#xff0c;第一种使用官方提供的launch screen.xib&#xff0c;这个直接看官方文档即可&#xff0c;这里不再多述&#xff1b;第二种方法是和之前iPhone5的类似&#xff0c;比较简单&#xff0c;为iPhone6和6添加两张特殊的pngiPhone6&…

【动态规划模型】金矿模型理解动态规划!(精彩的故事)

对于动态规划&#xff0c;每个刚接触的人都需要一段时间来理解&#xff0c;特别是第一次接触的时候总是想不通为什么这种方法可行&#xff0c;这篇文章就是为了帮助大家理解动态规划&#xff0c;并通过讲解基本的01背包问题来引导读者如何去思考动态规划。本文力求通俗易懂&…

android+图标+i_explore+无背景,Android Studio中Android Device Monitor中的File Explore不显示文...

环境&#xff1a;操作系统是Mac&#xff0c;模拟器问题&#xff1a;Android Studio中Android Device Monitor中的File Explore不显示文件本人在自学文件存储&#xff0c;想查看“dada/data”目录&#xff0c;本人在Windows上是可以在File Explore中看到的&#xff0c;在Mac上就…

origin怎么打开txt文本_【每日一学】差示扫描量热法(DSC)测量材料的比热容(3在Origin软件中计算间接法测得比热容的方法)...

在本系列内容第1部分和第2部分中分别介绍了使用DSC法通过间接法测量材料的比热容的常用方法的基本原理和得到高质量比热数据的方法&#xff0c;在完成实验后需要在相关的分析软件中计算所研究的材料的比热容。在目前大多数商品化的DSC仪所附带的分析软件中通常可以额外配置可用…

【HDU - 2809】 God of War(状压dp)

题干&#xff1a; At 184~280 A.D ,there were many kingdoms in China. Three strongest among them are "Wei", "Shu", "Wu". People call this period as "Three Kingdoms". HH is a super "Three Kingdoms" fan, beca…

mysql 优化配置 大批量数据插入_[译] MySQL 最佳实践 —— 高效插入数据

当你需要在 MySQL 数据库中批量插入数百万条数据时&#xff0c;你就会意识到&#xff0c;逐条发送 INSERT 语句并不是一个可行的方法。MySQL 文档中有些值得一读的 INSERT 优化技巧。在这篇文章里&#xff0c;我将概述高效加载数据到 MySQL 数据库的两大技术。LOAD DATA INFILE…

android 移植游戏,Unity游戏移植到Android平台

很多时候不仅需要单纯的运行单个的unity游戏&#xff0c;而是需要将游戏嵌入Android代码中和android其他功能相辅生成一个APP&#xff0c;比如通过android界面的一个按钮来启动一个unity游戏。本文介绍一下主要的过程。1. 将可运行的unity游戏打包为Android project。在Unity界…

code vs 代码格式化排版_23行代码,教你用python实现百度翻译!(建议收藏)

前言&#xff1a;努力折腾的人生虽然不是符合完美生活&#xff0c;但它一定是个很精彩的人生&#xff01;生命在于折腾&#xff0c;正如敲代码一样&#xff0c;你们说是吗&#xff1f;文章主要介绍了用23行python代码实现百度翻译&#xff0c;颇有参考性&#xff0c;喜欢的记得…

【HDU - 1013 】Digital Roots (大数模拟)

题干&#xff1a; The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summe…

mete30是鸿蒙系统么,华为mete30pro什么时候能用上鸿蒙系统?

[其他]华为mete30pro什么时候能用上鸿蒙系统&#xff1f;8957电梯直达huafans01303113614新学乍练发表于 2021-4-21 21:43:19来自&#xff1a;HUAWEI Mate 30 Pro 5G最新回复 2021-4-22 12:10:10华为mete30pro什么时候能用上鸿蒙系统&#xff1f;能有确切的时间吗伊凡爱尔顿已臻…

【HDU - 1520】Anniversary party (树形dp)

题干&#xff1a; There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted at the rector V. E. Tretyako…

android assets 文件夹 复制,Android 中 Assets目录下 文件或文件夹的复制

1、文件或文件夹的复制/** 下面两个方法不是AsyncTask的接口** copyFileOrDir 目录复制* copyFile 文件复制*/private void copyFileOrDir(String path) {AssetManager assetManager mContext.getAssets();String assets[] null;try {assets assetManager.list(path);//复制…

python运算符中用来计算整商的是什么_零基础学python,看完这篇文章,你的python基础就差不多了...

Python基础语法1. 认识Python1.1 Python 简介Python 的创始人为吉多范罗苏姆&#xff08;Guido van Rossum&#xff09;。Python 的设计目标&#xff1a;一门简单直观的语言并与主要竞争者一样强大开源&#xff0c;以便任何人都可以为它做贡献代码像纯英语那样容易理解适用于短…

*【HDU - 1242 】 Rescue (反向dfs,或bfs)

题干&#xff1a; Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M < 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angels friends want to save Angel. Their task is: approach Ang…

华为鸿蒙os系统转正,华为鸿蒙OS系统正式官宣,转正工作提上日程,明年多款终端将使用...

华为鸿蒙OS系统相信很多小伙伴都不陌生&#xff0c;作为国内现如今顶尖的科技企业。华为这些年的发展也是十分迅速的&#xff0c;而再快速的发展过程中。更多的用户对于华为的新款系统也充满了好奇&#xff0c;要知道一款属于国人自己的国产系统。在之前的国内手机上是几乎不存…

【FZU - 2140 】Forever 0.5 (计算几何,构造)

题干&#xff1a; Given an integer N, your task is to judge whether there exist N points in the plane such that satisfy the following conditions: 1. The distance between any two points is no greater than 1.0. 2. The distance between any point and the ori…

map型字段 mongodb_MongoDB极简教程

来源&#xff1a;我没有三颗心脏1.MongDB 简介MongoDB(来自于英文单词“Humongous”&#xff0c;中文含义为“庞大”)是可以应用于各种规模的企业、各个行业以及各类应用程序的开源数据库。作为一个适用于敏捷开发的数据库&#xff0c;MongoDB 的数据模式可以随着应用程序的发展…

html 如何改变图片形状,图形变换的三种方式是什么?

图形变换的三种方式1、平移平移&#xff0c;是指在同一平面内&#xff0c;将一个图形上的所有点都按照某个直线方向做相同距离的移动&#xff0c;这样的图形运动叫做图形的平移运动&#xff0c;简称平移。平移不改变图形的形状和大小。图形经过平移&#xff0c;对应线段相等&am…

springboot html压缩,springboot 请求响应压缩

官方文档原文&#xff1a;[https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/reference/htmlsingle/\#how-to-enable-http-response-compression][https_docs.spring.io_spring-boot_docs_2.1.5.RELEASE_reference_htmlsingle_how-to-enable-http-response-compression]…