poj1613Cave Raider(带限制的最短路+spfa)

来源:互联网 发布:淘宝客默认佣金是多少 编辑:程序博客网 时间:2024/05/18 03:35

题目链接:

huangjing

题意:
题意:有很多条轨道,但是这些轨道在特定的时间内会关闭,求出从起点到终点的最小时间。
思路:
【1】首先建图比较麻烦,最开始我模拟度数,但是一直是错的,看了几个小时还是错的,最后参考别人的,果断暴力,巧妙的引入now变量。。
【2】然后就是求最短路了。。时间很难求。。就是在一个地方比较难弄,就是这条路可以走。所以在开启时间和达到temp的时间中去最大值,如果最大值+过路的时间都小于关闭的时间,那么就肯定可以走,所以这时候就可以进行松弛了。。。那么最后这个问题就解决了。。。

题目:
Language:
Cave Raider
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 861 Accepted: 266

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 21 2 5 4 10 14 20 24 301 2 6 2 10 22 306 9 1 61 2 6 5 101 3 7 8 20 30 402 4 8 5 13 21 303 5 10 16 25 34 452 5 9 22 32 40 503 4 15 2 8 24 344 6 10 32 45 56 655 6 3 2 5 10 152 3 5 2 9 19 252 2 1 21 2 7 6 9 121 2 9 8 12 190

Sample Output

1655*

Source

Asia Kaohsiung 2003

代码:

#include<cstdio>#include<iostream>#include<vector>#include<algorithm>#include<cstring>#include<queue>#define INF 0x3f3f3f3fusing namespace std;const int maxn=50+10;int dis[maxn];int n,m,st,en;bool vis[maxn];struct Edge{    int to,time;    vector<int>door;};vector<Edge>vec[maxn];void read_graph(){    char s[100+10];    for(int i=0;i<maxn;i++)       vec[i].clear();    int u,v,w;    scanf("%d%d%d",&m,&st,&en);    for(int i=1;i<=m;i++)    {        Edge now;        scanf("%d%d%d",&u,&v,&w);        now.time=w;        getchar();        gets(s);        int x=0;        int len=strlen(s);        now.door.push_back(0);        for(int i=0;i<len;i++)        {            if(s[i]>='0'&&s[i]<='9')                x=x*10+s[i]-'0';            else            {               now.door.push_back(x);               x=0;            }        }        now.door.push_back(x);        now.door.push_back(INF);        now.to=u;        vec[v].push_back(now);        now.to=v;        vec[u].push_back(now);    }}void Spfa(){    queue<int>Q;    while(!Q.empty())  Q.pop();    memset(dis,0x3f,sizeof(dis));    memset(vis,false,sizeof(vis));    dis[st]=0;    vis[st]=true;    Q.push(st);    while(!Q.empty())    {        int temp=Q.front();        vis[temp]=false;        Q.pop();        for(int i=0;i<vec[temp].size();i++)        {            int  next=vec[temp][i].to;            int time=vec[temp][i].time;            int cnt=vec[temp][i].door.size();            bool open;            for(int j=1;j<cnt;j++)             {                if(j&1)  open=true;                else open=false;                int real_time=max(dis[temp],vec[temp][i].door[j-1]);                if(open&&real_time+time<=vec[temp][i].door[j])                {                    if(dis[next]>time+real_time)                    {                       dis[next]=time+real_time;                       if(!vis[next])                        {                           vis[next]=true;                           Q.push(next);                        }                    }                    break;               }            }        }    }    if(dis[en]==INF)        printf("*\n");    else        printf("%d\n",dis[en]);}int main(){    while(~scanf("%d",&n))    {        if(n==0)  return 0;        read_graph();        Spfa();    }    return 0;}



0 0
原创粉丝点击