poj 1613 Cave Raider (SPFA)

Cave Raider
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 744 Accepted: 230

Description

Afkiyia is a big mountain. Inside the mountain, there are many caves. These caves are connected by tunnels. Hidden in one of the caves is a terrorist leader. Each tunnel connects two caves. There could be more than one tunnels connect the same two caves. 
At the joint of a tunnel and a cave, there is a door. From time to time, the terrorists close a tunnel by shutting the two doors at the two ends, and "clean" the tunnel. It is still a mystery how they clean the tunnel. However, we know that if a person (or any living creature) is trapped in the tunnel when it is being cleaned, then the person (or the living creature) will die. After a cleaning of the tunnel is finished, the door will open, and the tunnel can be used again. 
Now the intelligence servicemen have found out which cave the leader is hiding,and moreover, they know the schedule of the cleaning of the tunnels. Jing Raider is going to go into the cave and catch the leader. You need to help him find a route so that he can get to that cave in the shortest time. Be careful not to be trapped in a tunnel when it is being cleaned.

Input

The input consists of a number of test cases. The 1st line of a test case contains four positive integers n,m, s, t, separated by at least one space, where n is the number of caves (numbered 1, 2, ... , n), m is the number of tunnels (numbered 1, 2, ... ,m), s is the cave where Jing is located at time 0, and t is the cave where the terrorist leader is hiding. (1 <= s, t <= n <= 50 and m <= 500). 
The next m lines are information of the m tunnels: Each line is a sequence of at most 35 integers separated by at least one space. The first two integers are the caves that are the ends of the corresponding tunnel. The third integer is the time needed to travel from one end of the tunnel to the other. This is followed by an increasing sequence of positive integers (each integer is at most 10000) which are alternately the closing and the opening times of the tunnel. For example, if the line is 
10 14 5 6 7 8 9 
then it means that the tunnel connects cave 10 and cave 14, it takes 5 units of time to go from one end to the other. The tunnel is closed at time 6, opened at time 7, then closed again at time 8, opened again at time 9. Note that the tunnel is being cleaned from time 6 to time 7, and then cleaned again from time 8 to time 9. After time 9, it remains open forever. 
If the line is 
10 9 15 8 18 23 
then it means that the tunnel connects cave 10 and cave 9, it takes 15 units of time to go from one end to the other. The tunnel is closed at time 8, opened at time 18,then closed again at time 23. After time 23, it remains closed forever. 
The next test case starts after the last line of the previous case. A 0 signals the end of the input.

Output

The output contains one line for each test case. Each line contains either an integer, which is the time needed for Jing to get to cave t, or the symbol *, which means that Jing can never get to cave t. Note that the starting time is 0. So if s = t, i.e., Jing is at the same cave as the terrorist leader, then the output is 0.

Sample Input

2 2 1 2
1 2 5 4 10 14 20 24 30
1 2 6 2 10 22 30
6 9 1 6
1 2 6 5 10
1 3 7 8 20 30 40
2 4 8 5 13 21 30
3 5 10 16 25 34 45
2 5 9 22 32 40 50
3 4 15 2 8 24 34
4 6 10 32 45 56 65
5 6 3 2 5 10 15
2 3 5 2 9 19 25
2 2 1 2
1 2 7 6 9 12
1 2 9 8 12 19
0

Sample Output

16
55
*

Source


ps:题目有点啰嗦,实际上想清楚了就是一个最短路问题。只不过方程“dist[v]>dist[u]+edge[u][v]”中的edge[u][v]不太好找而已。


思路:

SPFA,将边用vector[Node]存下来,要松弛谁时在一个一个找,找到最小的edge[u][v]。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define maxn 55
using namespace std;

const int INF=0x3f3f3f3f;
int n,m,ans,sx,ex;
bool vis[maxn];
int dist[maxn];
char s[1000005];
struct Node
{
    int w,sz;
    int a[40];
} cur,now;
vector<Node>v[maxn][maxn];
queue<int>q;

void init()
{
    int i,j;
    memset(vis,0,sizeof(vis));
    memset(dist,0x3f,sizeof(dist));
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=n; j++)
        {
            v[i][j].clear();
        }
    }
}
void read()
{
    int i,j,k=0,t=0,uu,vv,w,flag=0,len;
    len=strlen(s);
    s[len]='x';
    s[len+1]='\0';
    for(i=0; s[i]!='\0'; i++)
    {
        if(s[i]>='0'&&s[i]<='9')
        {
            flag=1;
            t=t*10+s[i]-'0';
        }
        else
        {
            if(flag)
            {
                k++;
                flag=0;
                if(k==1) uu=t;
                else if(k==2) vv=t;
                else if(k==3)
                {
                    w=t;
                    cur.w=w;
                }
                else cur.a[k-3]=t;
                t=0;
            }
        }
    }
    cur.a[0]=0;            // 将0加进去
    if((k-3)%2==0)         // 看情况将INF加进去
    {
        cur.a[k-2]=INF;
        cur.sz=k-2;
    }
    else cur.sz=k-3;
    v[uu][vv].push_back(cur);
    v[vv][uu].push_back(cur);
}
int getcost(int uu,int vv,int k,int costu)
{
    int i,j,sz;
    sz=v[uu][vv][k].sz;
    for(i=0; i<=sz; i+=2)
    {
        if(costu>v[uu][vv][k].a[i+1]) continue ;
        else if(costu<=v[uu][vv][k].a[i+1]&&costu>=v[uu][vv][k].a[i])
        {
            if(costu+v[uu][vv][k].w<=v[uu][vv][k].a[i+1])
            {
                return costu+v[uu][vv][k].w;
            }
        }
        else
        {
            if((v[uu][vv][k].a[i+1]-v[uu][vv][k].a[i])>=v[uu][vv][k].w)
            {
                return v[uu][vv][k].a[i]+v[uu][vv][k].w;
            }
        }
    }
    return -1;
}
void SPFA()
{
    int i,j,nx,sz,mi,cost;
    memset(vis,0,sizeof(vis));
    while(!q.empty()) q.pop();
    dist[sx]=0;
    vis[sx]=1;
    q.push(sx);
    while(!q.empty())
    {
        nx=q.front();
        q.pop();
        vis[nx]=0;
        for(i=1; i<=n; i++) // 找到与之相邻的边
        {
            sz=v[nx][i].size();
            if(!sz) continue ;
            mi=INF;
            for(j=0; j<sz; j++)   // 找到最小的edge[u][v]
            {
                cost=getcost(nx,i,j,dist[nx]);
                if(cost!=-1&&mi>cost) mi=cost;
            }
            if(dist[i]>mi)
            {
                dist[i]=mi;
                if(!vis[i])
                {
                    vis[i]=1;
                    q.push(i);
                }
            }
        }
    }
}
int main()
{
    int i,j,t;
    while(scanf("%d",&n),n)
    {
        scanf("%d%d%d",&m,&sx,&ex);
        init();
        gets(s);
        for(i=1; i<=m; i++)
        {
            gets(s);
            read();
        }
        SPFA();
        if(dist[ex]<INF) printf("%d\n",dist[ex]);
        else printf("*\n");
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值