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

题目链接:

huangjing

题意:

题意:有很多条轨道,但是这些轨道在特定的时间内会关闭,求出从起点到终点的最小时间。

思路:

【1】首先建图比较麻烦,最开始我模拟度数,但是一直是错的,看了几个小时还是错的,最后参考别人的,果断暴力,巧妙的引入now变量。。

【2】然后就是求最短路了。。时间很难求。。就是在一个地方比较难弄,就是这条路可以走。所以在开启时间和达到temp的时间中去最大值,如果最大值+过路的时间都小于关闭的时间,那么就肯定可以走,所以这时候就可以进行松弛了。。。那么最后这个问题就解决了。。。

题目:


Language:
Default

Cave Raider

Time Limit: 1000MS   Memory Limit: 10000K
Total 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 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

Asia Kaohsiung 2003

代码:

#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<queue>
#define INF 0x3f3f3f3f
using 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;
}

时间: 2024-08-21 13:20:46

poj1613Cave Raider(带限制的最短路+spfa)的相关文章

最短路SPFA 算法详解

最短路SPFA 算法详解 适用范围:给定的图存在负权边,这时类似Dijkstra等算法便没有了用武之地,而Bellman-Ford算法的复杂度又过高,SPFA算法便派上用场了. 我们约定有向加权图G不存在负权回路,即最短路径一定存在.当然,我们可以在执行该算法前做一次拓扑排序,以判断是否存在负权回路,但这不是我们讨论的重点. 算法思想:我们用数组d记录每个结点的最短路径估计值,用邻接表来存储图G.我们采取的方法是动态逼近法:设立一个先进先出的队列用来保存待优化的结点,优化时每次取出队首结点u,并

最短路 spfa算法

问题描述 给定一个n个顶点,m条边的有向图(其中某些边权可能为负,但保证没有负环).请你计算从1号点到其他点的最短路(顶点从1到n编号). 输入格式 第一行两个整数n, m. 接下来的m行,每行有三个整数u, v, l,表示u到v有一条长度为l的边. 输出格式 共n-1行,第i行表示1号点到i+1号点的最短路. 样例输入 3 31 2 -12 3 -13 1 2 样例输出 -1-2 数据规模与约定 对于10%的数据,n = 2,m = 2. 对于30%的数据,n <= 5,m <= 10. 对

TOJ--2674--最短路(spfa)

厌死了......排位 晋级赛 两连跪 ... 三角形 的那题还是 无限WA  ... 还有 明天又要早起... 先还是来看下这题吧 话说 好久没写 最短路了 --------- spfa 是我最喜欢的最短路版本 touch me 这题 其实相比其它的最短路 还是有个很让人看不懂的地方---让我纠结了很久 首先 它告诉了我们城市的编号 是从 1~1000 然后 又突然告诉我们 哪些城市和我们的出发地相邻 假如是 1 10 100...之类的 最后 告诉我们 你想取哪些城市 我就在想 起点呢!!!

洛谷P1462 通往奥格瑞玛的道路 二分答案+最短路SPFA

洛谷P1462 通往奥格瑞玛的道路二分答案+最短路SPFA 二分交费最多的一次的钱数 然后只将符合要求的边加入图中 如果到终点的最短路大于等于血量 或者直接起点不能到达终点那么说明不符合要求 需要加大答案 时间复杂度 (log答案)* Ek 需要注意如果本来就不能到达 那么直接输出AFK 1 #include <bits/stdc++.h> 2 #define LL long long 3 #define For(i,j,k) for(int i=j;i<=k;i++) 4 using

hdu 2962 Trucking (二分+最短路Spfa)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1763    Accepted Submission(s): 618 Problem Description A certain local trucking co

nyoj1006(最短路次短路spfa)

偷西瓜 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 对于农村的孩子来说最大的乐趣,莫过于和小伙伴们一块下地偷西瓜了,虽然孩子们条件不是很好,但是往往他们很聪明,他们总在计算着到达瓜田的距离,以及逃跑的路线,他们总是以最短的距离冲到瓜田里面,然后以最短的距离回到出发的地方,不过瓜田的大人们已经在他们来的路上等待他们.于是聪明的小伙伴们便不走过的路,即每条路只走一遍,如果小伙伴们回不到出发的地方,他们就说"eating", 我们假设 有 n (n<=

POJ3255:Roadblocks(次短路 SPFA+A星)

给出1-N 个点 的距离, 求从1号到N号的次短路, 直接用k短路来做了,,dj会TLE, 用spfa就过了 题目: I - RoadblocksTime Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Bessie has moved to a small farm and sometimes enjoys returning to visit o

It&amp;#39;s not a Bug, It&amp;#39;s a Feature! (poj 1482 最短路SPFA+隐式图+位运算)

Language: Default It's not a Bug, It's a Feature! Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 1353   Accepted: 516 Description It is a curious fact that consumers buying a new software product generally do not expect the software to

uva 10099 The Tourist Guide(单源最短路/spfa/dijkstra)

题目: 链接:点击打开链接 题意: 思路: 代码: #include <iostream> #include <cstring> #include <cstdio> using namespace std; int map[101][101]; void floyd(int n) { for(int k=1; k<=n; k++) for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) map[i][j] = m