POJ3259 Wormholes【判斷是否有負環】
Wormholes
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 65655 | Accepted: 24488 |
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..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 Tseconds 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
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
問題連結:POJ3259 Wormholes
問題描述:FJ有F個農場,每個農莊裡有N塊地,有M條雙向邊:(S,E,T)表示S和E之間有一條雙向邊,需要T時間才能走完。
還有W個蟲洞:(S,E,T)表示從S到E有一條單向邊,從S到E會使得時間倒退T時間。問在每個農場裡FJ能不能回到比開始還早的時間,如果可以輸出YES,否則輸出NO。
解題思路:實際上是求是否存在負環。
AC的C++程式:
Floyd演算法判斷是否有負環
#include<iostream>
using namespace std;
const int N=505;
const int INF=10000000;
int dp[N][N];
//floyd演算法判斷是否存在負環
bool floyd(int n)
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)
if(dp[i][j]>dp[i][k]+dp[k][j])
dp[i][j]=dp[i][k]+dp[k][j];
if(dp[i][i]<0)
return true;
}
return false;
}
int main()
{
int F,N,M,W,a,b,t;
scanf("%d",&F);
while(F--){
scanf("%d%d%d",&N,&M,&W);
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
dp[i][j]=(i==j)?0:INF;
while(M--){
//雙向邊
scanf("%d%d%d",&a,&b,&t);
if(t<dp[a][b])
dp[a][b]=dp[b][a]=t;
}
while(W--){
//單向邊
scanf("%d%d%d",&a,&b,&t);
dp[a][b]=-t;//負號表示使時間倒退-t
}
if(floyd(N))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
Bellman_ford演算法判斷是否有負環
#include<iostream>
#include<cstring>
using namespace std;
int dist[505];
const int INF=10000000;
struct Edge{
int start,end,w;//從u到v有一條花費為w的路
}edge[6000];
//n個結點 m條邊
bool Bellman_ford(int n,int m)
{
memset(dist,INF,sizeof(dist));
dist[1]=0;
//進行n-1次鬆弛操作
for(int i=1;i<n;i++)
for(int j=0;j<m;j++)
if(dist[edge[j].end]>dist[edge[j].start]+edge[j].w)
dist[edge[j].end]=dist[edge[j].start]+edge[j].w;
//判斷是否存在負環
for(int j=0;j<m;j++)
if(dist[edge[j].end]>dist[edge[j].start]+edge[j].w)
return true;
return false;
}
int main()
{
int F,N,M,W,a,b,t;
scanf("%d",&F);
while(F--){
scanf("%d%d%d",&N,&M,&W);
int k=0;
while(M--){
//雙向邊
scanf("%d%d%d",&a,&b,&t);
edge[k].start=a,edge[k].end=b,edge[k].w=t;
k++;
edge[k].start=b,edge[k].end=a,edge[k].w=t;
k++;
}
while(W--){
//單向邊
scanf("%d%d%d",&a,&b,&t);
edge[k].start=a,edge[k].end=b,edge[k].w=-t;
k++;
}
bool flag=Bellman_ford(N,k);
printf("%s\n",flag?"YES":"NO");
}
return 0;
}
SPFA演算法判斷是否有負環:
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=505;
struct Edge{//邊
int v,cost;//起點到終點v的花費為cost
Edge(int v,int cost):v(v),cost(cost){}
};
int dist[N];//距離
bool vis[N];//標記結點是否進佇列
int cnt[N];//標記結點進佇列的次數
vector<Edge>g[N];//圖的鄰接表表示
bool spfa(int n,int s)//n是結點的個數,s是源點
{
queue<int>q;
for(int i=0;i<=n;i++){
vis[i]=false;
cnt[i]=0;
dist[i]=INF;
}
vis[s]=true;
dist[s]=0;
cnt[s]=1;
q.push(s);
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=false;//出佇列標記為false
int num=g[u].size();//以u為起點的終點個數
for(int i=0;i<num;i++){
int v=g[u][i].v;//終點編號
if(dist[u]+g[u][i].cost<dist[v]){
dist[v]=dist[u]+g[u][i].cost;
if(!vis[v]){
q.push(v);
vis[v]=true;
cnt[v]++;
if(cnt[v]>=n)
return false;
}
}
}
}
return true;
}
int main()
{
int T,n,m,w,a,b,t;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&w);
for(int i=0;i<=n;i++)
g[i].clear();
while(m--){
scanf("%d%d%d",&a,&b,&t);
g[a].push_back(Edge(b,t));
g[b].push_back(Edge(a,t));
}
while(w--){
scanf("%d%d%d",&a,&b,&t);
g[a].push_back(Edge(b,-t));
}
int flag=spfa(n,1);
if(flag)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}
相關文章
- POJ 3259-Wormholes(簡單判負環)Worm
- 判斷一個有向圖是否有環
- 判斷負環模板
- 對一個連結串列判斷是否有環
- 如何判斷連結串列中是否有環並找出環的入口位置
- 判斷是否為環形連結串列
- 判斷欄位中是否有漢字
- 如何判斷一項技術是否有前途?
- JavaScript 判斷物件中是否有某屬性JavaScript物件
- POJ1860 Currency Exchange【Bellman_ford演算法:判斷是否有正環】演算法
- python如何判斷一列是否有資料Python
- 判斷字串是否為空字串
- python 判斷是否為中文Python
- 判斷字串是否唯一字串
- 判斷URL字串是否合法字串
- python判斷是否為listPython
- js判斷物件裡面是否有某個屬性JS物件
- 判斷一個物件是否為空物件,判斷一個物件中是否有空值物件
- JavaScript判斷字串是否為空JavaScript字串
- js判斷物件是否為空JS物件
- js判斷checkbox是否選中JS
- jQuery 判斷元素是否隱藏jQuery
- JavaScript 判斷是否是陣列JavaScript陣列
- 判斷網路是否連線
- JavaScript 判斷函式是否存在JavaScript函式
- golang判斷檔案是否存在Golang
- MySQL判斷表名是否存在MySql
- QJsonObject判斷欄位是否存在JSONObject
- java判斷物件是否為空Java物件
- Delphi Variant 判斷是否為空
- 判斷協議是否出網協議
- mysql如何判斷是否為空MySql
- postgresql如何判斷表是否存在SQL
- js判斷字串是否為空JS字串
- Activiti判斷流程是否結束
- python 判斷檔案是否存在Python
- java判斷字串是否為空Java字串
- 判斷單連結串列中是否存在環,並輸出環入口節點。