洛谷 P2850 [USACO06DEC]虫洞Wormholes 题解【负环】【SPFA】
点击量:166
题面里坑太多了唔?
题目描述
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.
John在他的农场中闲逛时发现了许多虫洞。虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前)。John的每个农场有M条小路(无向边)连接着N (从1..N标号)块地,并有W个虫洞。其中1<=N<=500,1<=M<=2500,1<=W<=200。 现在John想借助这些虫洞来回到过去(出发时刻之前),请你告诉他能办到吗。 John将向你提供F(1<=F<=5)个农场的地图。没有小路会耗费你超过10000秒的时间,当然也没有虫洞回帮你回到超过10000秒以前。
输入输出格式
输入格式:
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.
输出格式:
Lines 1..F: For each farm, output “YES” if FJ can achieve his goal, otherwise output “NO” (do not include the quotes).
输入输出样例
输入样例#1:23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8输出样例#1:NOYES说明
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.
解法:
bulubula说了一大堆,其实就是让我们找一个负环。题目中没有规定要从哪个点进去,只是说要回到过去,隐藏了要回到出发点这一信息,那么我们就要枚举出发点,做SPFA判负环。因为一旦有负环,从负环上的一点进去,一定可以回到过去。这里不用什么玄学操作,只用管一个点是否进队n次。(我直接用了所有点进队n×n次…)
这个题目还是比较基础的。
Code:
#include<cstdio>
#include<cstring>
#include<queue>
using std::deque;
struct node
{
int n,v;
node *nxt;
node (int n,int v)
{
this->n=n;
this->v=v;
nxt=NULL;
}
node()
{
nxt=NULL;
}
};
node head[511],*tail[511];
int dis[511],n;
bool vis[511],used[511];
int cnt=0;
bool spfa(int x)
{
memset(used,0,sizeof(used));
memset(dis,0x3f,sizeof(dis));
used[x]=true;
dis[x]=0;
deque<int> q;
q.push_back(x);
vis[x]=true;
cnt=0;
while(!q.empty())
{
int k=q.front();
q.pop_front();
used[k]=false;
node *p=&head[k];
while(p->nxt!=NULL)
{
p=p->nxt;
if(dis[k]+p->v<dis[p->n])
{
dis[p->n]=dis[k]+p->v;
cnt++;
if(cnt>=n*n)//判负环
return true;
if(!used[p->n])
{
used[p->n]=true;
vis[p->n]=true;
if(q.empty()||dis[p->n]<dis[q.front()])
q.push_front(p->n);
else
q.push_back(p->n);
}
}
}
}
return dis[1]<0;
}
int main()
{
int m,w,u,v,h;
int T;
scanf("%d",&T);
while(T--)
{
memset(dis,0x3f,sizeof(dis));
scanf("%d%d%d",&n,&m,&h);
for(int i=1;i<=n;i++)
tail[i]=&head[i];
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
tail[u]->nxt=new node(v,w);
tail[u]=tail[u]->nxt;
tail[v]->nxt=new node(u,w);
tail[v]=tail[v]->nxt;
}
for(int i=1;i<=h;i++)
{
scanf("%d%d%d",&u,&v,&w);
tail[u]->nxt=new node(v,-w);
tail[u]=tail[u]->nxt;
}
memset(vis,0,sizeof(vis));
int flag=0;
for(int i=1;i<=n;i++)
if(!vis[i])
if(spfa(i))
flag=1;
if(flag)
puts("YES");
else
puts("NO");
}
return 0;
}
说点什么