Wormholes——Bellman-Ford判斷負環

【題目描述】

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…N, M (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, F. F farm descriptions follow.
Line 1 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2… M+1 of each farm: Three space-separated numbers ( S, E, T) that describe, respectively: a bidirectional path between S and E that 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 ( S, E, T) 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

【題目分析】
很正常的判斷負環,而且就算有重邊也并不影響。
【AC代碼】

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<climits>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;typedef long long ll;
const int MAXN=505;
const int MAXM=6005;
struct node
{int u,v,w;
}edge[MAXM],E;
int tot;
int n,m,k;
int dis[MAXN];inline void AddEdge(int u,int v,int w)
{edge[tot].u=u; edge[tot].v=v; edge[tot].w=w;tot++;
}bool Bellman()
{memset(dis,0x3f,sizeof(dis));dis[1]=0;bool flag;int T=n;while(T--){flag=false;for(int i=0;i<tot;i++){E=edge[i];if(dis[E.v]>dis[E.u]+E.w){dis[E.v]=dis[E.u]+E.w;flag=true;}}if(!flag) break;}return T==-1;	//T的值最后是-1,我還以為是0.。。。
}int main()
{int T,u,v,w;scanf("%d",&T);while(T--){scanf("%d%d%d",&n,&m,&k);tot=0;for(int i=0;i<m;i++){scanf("%d%d%d",&u,&v,&w);AddEdge(u,v,w); AddEdge(v,u,w);}for(int i=0;i<k;i++){scanf("%d%d%d",&u,&v,&w);AddEdge(u,v,-w);}if(Bellman()){printf("YES\n");}else{printf("NO\n");}}return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384085.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384085.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384085.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C++11 標準新特性:Defaulted 和 Deleted 函數

https://www.ibm.com/developerworks/cn/aix/library/1212_lufang_c11new/index.html Defaulted 函數 背景問題 C 的類有四類特殊成員函數&#xff0c;它們分別是&#xff1a;默認構造函數、析構函數、拷貝構造函數以及拷貝賦值運算符。這些類的特殊成員函數負責創建、初始化、…

順序表實現棧相關操作

1.棧的相關概念 棧是一種特殊的線性表, 其中只允許在固定的一端進行插入和刪除元素.進行數據插入和刪除的一端叫做棧頂, 另一端成為棧底. 不含任何元素的棧稱為空棧, 棧又稱為先進先出的線性表. 2. 順序棧的結構 3. 順序棧的具體操作 (1). 數據結構 typedef char SeqStackTyp…

MPI Maelstrom——Dijkstra

【題目描述】 BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKee’s research advisor, Jack Swigert, has asked her to bench…

雙向帶環帶頭結點的鏈表實現棧

1. 數據結構 利用帶頭結點帶環的結點實現棧的相關操作.因此, 每一個結點包括了一個前驅, 一個后繼, 還有一個數據成員 typedef char DLinkStackType;typedef struct DLinkStack {DLinkStackType data;struct DLinkStack* next;struct DLinkStack* prev; }DLinkStack;2. 初始化…

Cow Contest——Floyed+連通性判斷

【題目描述】 N (1 ≤ N ≤ 100) cows, conveniently numbered 1…N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors. The contest …

C++11 標準新特性:委派構造函數

https://www.ibm.com/developerworks/cn/rational/1508_chenjing_c11/index.html陳 晶2015 年 8 月 11 日發布WeiboGoogle用電子郵件發送本頁面 1本文首先介紹了在委派構造函數提出之前類成員構造所面臨的問題&#xff0c;再結合實例介紹了委派構造函數的用法&#xff0c;并說明…

順序表實現隊列

一. 隊列相關概念 隊列是只允許在一段進行插入元素, 在另一端進行刪除元素的線性表,即只允許對隊列進行尾插,頭刪的操作.隊列具有先進先出, 后進后出的特性. ???????? 1.初始化 void SeqQueInit(SeqQue* q) {if(q NULL){return;//非法輸入}q -> head 0;q -> …

Arbitrage——判斷正環Bellman-Ford/SPFA

【題目描述】 Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French …

鏈表實現隊列

上篇博客是用順序表實現隊列, 現在用雙向帶頭結點帶環鏈表實現對隊列的出隊列, 入隊列, 取隊首元素, 以及銷毀隊列的相關操作 1.初始化鏈表 void DLinkQueInit(DLinkQue** q) {if(q NULL){return;//非法輸入}if(*q NULL){return;//非法輸入帶頭結點的鏈表至少有一個傀儡結點…

HDU - 1796——容斥原理+二進制枚舉

【題目描述】 Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N12, and M-integer set is {2,3}, so there is another set {2,3,4,…

數據結構學習(二)——單鏈表的操作之頭插法和尾插法創建鏈表

http://blog.csdn.net/abclixu123/article/details/8210109 鏈表也是線性表的一種&#xff0c;與順序表不同的是&#xff0c;它在內存中不是連續存放的。在C語言中&#xff0c;鏈表是通過指針相關實現的。而單鏈表是鏈表的其中一種&#xff0c;關于單鏈表就是其節點中有數據域和…

信號的基本概念以及信號的產生

一. 信號產生的場景 1. 用戶輸入命令, 在shell 啟動一個前臺進程 ???? 2. 當用戶按一下 Ctrl C 的時候,從鍵盤產生一個硬件中斷 ???? 3. 此時CPU 正在執行這個進程的帶代碼, 則該進程的執行代碼暫停執行, CPU 從用戶態切換到內核態處理該硬件中斷. ???? 4. 中斷…

HDU - 1028——母函數入門

【題目描述】 “Well, it seems the first problem is too easy. I will let you know how foolish you are later.” feng5166 says. “The second problem is, given an positive integer N, we define an equation like this: Na[1]a[2]a[3]…a[m]; a[i]>0,1<m<N;…

信號的阻塞

一. 阻塞信號 1.信號的相關概念 ????(1) 遞達: 實際執行信號的處理動作稱為信號的遞達 ????(2) 未決: 信號從產生到遞達之間的過程叫做信號的未決 ????(3) 阻塞: 進程可以選擇阻塞某個信號, 被阻塞的信號產生時將保持在未決狀態, 直到進程解除該信號的屏蔽, 才…

POJ 1511 Invitation Cards——Dijkstra優先隊列優化+反向建圖

【題目描述】 In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the…

鏈棧 尹成

http://blog.csdn.net/itcastcpp/article/details/39271661 今天&#xff0c;我們一起用C寫鏈棧&#xff0c;具體如下。 LinkStack.h具體內容&#xff1a; [cpp] view plaincopy #include "StackNode.h" template<typename Type> class LinkStack{ publ…

信號的捕捉以及SIGCHLD信號

一. 信號的捕捉定義 用戶提供一個處理函數, 要求內核在處理信號時必須切換到用戶態,執行這個函數, 這種方式就叫做信號的捕捉 二. 圖解信號的捕捉過程 1. 由上圖可以看出,當處理信號的執行動作時用戶自定義的時候,此時就需返回該函數去調用該函數, 這就叫做信號的捕捉. 以前我…

鏈隊 尹成

http://blog.csdn.net/itcastcpp/article/details/39271691 今天&#xff0c;我們一起用C寫一個鏈對&#xff0c;具體如下所示。 LinkQueue.h具體內容如下&#xff1a; [cpp] view plaincopy #include "QueueNode.h" template<typename Type> class LinkQueu…

強連通分量入門——Trajan算法

今天學習了強連通分量。 【參考博客】 如果覺得我講的有些地方難以理解或者存在問題&#xff08;歡迎留言&#xff09;&#xff0c;可以看一下我借鑒的一些大佬的博客&#xff1a; 傳送門1 傳送門2 【知識儲備】 首先我們需要對幾個定義有一些概念&#xff1a; 強連通&#xff…

最小棧的實現

所謂最小棧, 就是當前棧頂元素最小, 我們可以這樣做, 每次在入棧之前, 將待入棧元素與棧頂元素相比, 每次現將待入棧的元素先入棧, 再將帶入棧的元素和較小的元素入棧, 這樣就可以保證每次棧頂元素是最小元素. 在出棧的時候規定每次出棧兩個元素,這樣就可以保證在出棧之后棧頂元…