POJ3259(最经典的SPFA)

24 篇文章 0 订阅

POJ3259【Wormholes】

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 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
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

USACO 2006 December Gold


做完才知道为什么说这道题是”最经典的SPFA“,这道题巧妙的利用spfa可以解决负边权的特性,优化了Dijkstra的不足,同时提高了效率。是解决单源最短路径的一种非常好的方法。

算法描述

  • 1.定义一个队列保存待优化的结点,把源点加入队列。
  • 2.取出队首结点x,
  • 3.用x点当前的最短路径估计值F[x],对离开x点所指向的结点y进行松弛操作.
  • 4.如果y点的最短路径估计值F[y]被更新,且y点不在当前的队列中,就将y点放入队尾。
  • 5.重复2直至队列空为止。
其实这东西也贼简单。。

Spfa

SPFA(Shortest Path Faster Algorithm)(队列优化)算法是求单源最短路径的一种算法,在Bellman-ford算法的基础上加上一个队列优化,减少了冗余的松弛操作,是一种高效的最短路算法。 —— [ 百度百科 ]

引用一下老师的PPT:

图G有n个结点,m条边。
SPFA可以处理负边,SPFA的实现甚至比Dijkstra还要简单,当然不能和floyed比。
SPFA可以在O(M)的时间复杂度内求出源点到其他所有点的最短路径。
实际上期望的时间复杂度:O(kM), 其中k为所有顶点进队的平均次数,可以证明k一般小于等于2。
适用于边相对稀疏的图,因为算法的时间复杂度只与边的个数有关系。
Dijkstra可以在O(n^2)的时间复杂度内求出源点到其他所有点的最短路径。适用于边相对稠密的图,因为算法的时间复杂度与m没有关系,只与顶点个数有关。
Floyed算法的时间复杂度是O(n^3),当然它求的是所有点间的最短路径问题。


So..How about this problem?

方法:

判断负权环 我们可以… :

选择比较
记录每个顶点的入队次数,如果大于N,那么一定有负权环简单但是很慢
spfa类似于宽搜,记录宽搜的深度deep,如果deep>n则存在负权环很快
专门可以处理最短路带负权环的算法叫bellman-ford算法专门解决此类问题

题目大意是说虫洞能使时间倒流,问是否存在一条路径使得通过虫洞的时间倒流能够回到出发点,并且能够看到出发前的自己,虫洞是单向的,其他边都是双向的。此题可以看成求图中是否存在负权回路(负权回路:也叫复权环,就是环上的边权和为负的环,这样的环存在那么求最短路时就会死循环)的问题。

方法一【记录入队次数】:

#include<stdio.h>
#include<string.h>
#include<queue>
#define MAXN 5200
using namespace std;
struct node
{
    int to,next,val;
}edge[MAXN];
int cnt,n,m,w;
int head[MAXN],visit[MAXN],dis[MAXN],hash[MAXN];
void init()
{
    memset(head,-1,sizeof(head));
    memset(hash,0,sizeof(hash));
    cnt=1;
}
void addedge(int from,int to,int val)
{
    edge[cnt].to=to;
    edge[cnt].val=val;
    edge[cnt].next=head[from];
    head[from]=cnt++;
}
int spfa()
{
    memset(dis,0x3f,sizeof(dis));
    memset(visit,0,sizeof(visit));
    dis[1]=0;
    visit[1]=1;
    queue<int>q;
    q.push(1);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        visit[u]=0;
        hash[u]++;
        if(hash[u]>m)
        {
            return 1;
        }
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(dis[u]+edge[i].val<dis[v])
            {
                dis[v]=dis[u]+edge[i].val;
                if(!visit[v])
                {
                    visit[v]=1;
                    q.push(v);
                }
            }
        }
    }
    return 0;
}
int main()
{
    int f;
    scanf("%d",&f);
    while(f--)
    {
        init();
        scanf("%d%d%d",&n,&m,&w);
        for(int i=1;i<=m;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            addedge(a,b,c);
            addedge(b,a,c);
        }
        for(int i=1;i<=w;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            addedge(a,b,-c);
        }
        if(spfa())
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

方法二【Bellman_Ford】:

#include<stdio.h>
#include<string.h>
#include<queue>
#define MAXN 5200
using namespace std;
struct node
{
    int from,to,next,val;
}edge[MAXN];
int cnt,n,m,w;
int head[MAXN],visit[MAXN],dis[MAXN],hash[MAXN];
void init()
{
    memset(head,-1,sizeof(head));
    memset(hash,0,sizeof(hash));
    cnt=1;
}
void addedge(int from,int to,int val)
{
    edge[cnt].from=from;
    edge[cnt].to=to;
    edge[cnt].val=val;
    edge[cnt].next=head[from];
    head[from]=cnt++;
}
bool Bellman_Ford()  
{  
    bool flag;  
    dis[1]=0;  
    for(int i=1;i<=n;i++)  
    {  
        flag=false;  
        for(int j=1;j<=cnt;j++)  
        if(dis[edge[j].from]+edge[j].val<dis[edge[j].to])  
        {  
            dis[edge[j].to]=dis[edge[j].from]+edge[j].val;  
            flag=true;  
        }  
        if(!flag)          
        return false;  
    }  
    for(int i=1;i<=cnt;i++)  
    if(dis[edge[i].from]+edge[i].val<dis[edge[i].to])  
    return true; 
}  
int main()
{
    int f;
    scanf("%d",&f);
    while(f--)
    {
        init();
        scanf("%d%d%d",&n,&m,&w);
        for(int i=1;i<=m;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            addedge(a,b,c);
            addedge(b,a,c);
        }
        for(int i=1;i<=w;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            addedge(a,b,-c);
        }
        if(Bellman_Ford())
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

Attention:

1、如果使用手写队列实现spfa那么队列的长度要开的非常大,因为负权环的存在,使得结点频繁的进入队列。
2、边的总数:2500条无向边,200条有向边,那么2500*2+200=5200.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值