HDU-2437-Jerboas(BFS+优先队列)

Problem Description

Jerboas are small desert-living animals, which resemble mice with a long tufted tail and very long hind legs. Jerboas shelter in well-hidden burrows. They create two types of burrow: temporary and permanent. The temporary burrows
are plain tubes while the permanent burrows are sealed with a plug of sand to keep heat out and moisture in.

As far as we know, jerboa burrows in the desert are connected with one-way tunnels. What‘s more, for some unknown reasons, it‘s true that start from any burrow, follows the tunnels you can not go back to the starting burrow.

Summer means last-minute of offers on good times, so of course jerboas could not stay behind. One day, a little jerboa Alice who lived in a temporary burrow S wants to migrate to a permanent one. There are different routes she can take, but Alice is so
odd that she only selects those whose total travel distances is a multiple of K. Among all routes that Alice may select, we are interested in the shortest one. Can you help to find it out? Of course different routes may lead to different destinations.

Input

On the first line of input, there is a single positive integer T <= 20 specifying the number of test cases to follow.

Each test case starts with four integers in the first line: N, M, S, K.

N is the number of burrows in the desert (burrows are numbered with 1, 2, …, N);

M is the number of tunnels connecting the burrows;

S is where Alice lived and K is as described above.

(0 < N <= 1000, 0 <= M <= 20000, 0 < S <= N, 0 < K <= 1000)

The second line contains N characters each could be ‘T’ or ‘P’. The i-th character specifying the type of the burrow i. ‘T’ means temporary burrow, ‘P’ means permanent burrow. It’s guaranteed that the S-th character is ‘T’.

Next follow M lines, each line with 3 integers A, B, C. Specifying that there is a tunnel from burrow A to burrow B, and its length is C.

(0 < A, B <= N, A != B, 0 < C < 40000)

Output

For each test case you should output a single line containing "Case X: Y Z" (quotes for clarity) where X is the number of the test case (starting at 1) and Y is the length of the shortest route Alice can select and Z is the
destination of the selected route.

Notice that burrow Z should be a permanent burrow.

In case there’s more than one solution, Z should be the minimum.

In case there‘s no solution, Y and Z should be both equal to -1.

Sample Input

2
5 5 1 7
TPPTP
1 2 8
1 4 7
4 3 9
2 3 6
1 5 3
5 5 1 7
TPTTP
1 2 8
1 4 7
4 3 9
2 3 6
1 5 3

Sample Output

Case 1: 14 3
Case 2: -1 -1

Source

2008 Asia Chengdu Regional Contest Online

思路:BFS+优先队列。因为要求最小值,出队的时候必须保证最小的先出队,要用优先队列来维护。入队的时候没有简单的判重,而是让比当前状态距离小的入队。

#include <cstdio>
#include <queue>
#include <map>
#define INF 999999999
using namespace std;

struct S{
int pos;
long long dis;

bool operator<(const S & p) const
{
    return dis>p.dis;
}
}t;

char mp[1005];
int head[1005],nxt[20000],val[20000],des[20000],vis[1005][1000];

int main()
{
    int T,n,m,s,k,i,j,u,v,w,casenum=1,disans,desans;

    scanf("%d",&T);

    while(T--)
    {
        scanf("%d%d%d%d",&n,&m,&s,&k);

        scanf("%s",mp);

        for(i=0;i<=n;i++) head[i]=-1;

        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);

            nxt[i]=head[u];
            head[u]=i;
            val[i]=w;
            des[i]=v;
        }

        printf("Case %d: ",casenum++);

        priority_queue<S>que;
        for(i=1;i<=n;i++) for(j=0;j<k;j++) vis[i][j]=0;

        vis[s][0]=1;

        disans=desans=INF;

        t.dis=0;
        t.pos=s;

        que.push(t);

        while(!que.empty())
        {
            t=que.top();
            que.pop();

            if(t.dis && mp[t.pos-1]=='P' && t.dis%k==0 && t.dis<=disans)
            {
                disans=t.dis;

                if(t.pos<desans) desans=t.pos;
            }

            for(i=head[t.pos];i!=-1;i=nxt[i])
            {
                t.dis+=val[i];
                t.pos=des[i];

                if(!vis[t.pos][t.dis%k] || t.dis<vis[t.pos][t.dis%k])
                {
                    vis[t.pos][t.dis%k]=t.dis;

                    que.push(t);
                }

                t.dis-=val[i];
            }
        }

        if(disans==INF) printf("-1 -1\n");
        else printf("%d %d\n",disans,desans);
    }

}

HDU-2437-Jerboas(BFS+优先队列)

时间: 2024-10-29 19:09:47

HDU-2437-Jerboas(BFS+优先队列)的相关文章

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

hdu 1242 Rescue(bfs+优先队列)

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approach Angel. We assume

HDU 2425-Hiking Trip(BFS+优先队列)

Hiking Trip Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1303    Accepted Submission(s): 576 Problem Description Hiking in the mountains is seldom an easy task for most people, as it is extr

HDU 1242 (BFS+优先队列)

题意:X代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙,走过.要花费一秒,走过x要花费2秒,求从起点到终点的最少时间. 析:一看到样例就知道是BFS了吧,很明显是最短路径问题,不过又加了一个条件——时间,所以我们用优先队列去优先获取时间短的路径,总体实现起来没有太大难度. 代码如下: #include <iostream> #include <cstdio> #include <vector> #include <set> #include <

hdu 1242 Rescue (BFS+优先队列)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1242 这道题目我是用BFS+优先队列做的.听说只用bfs会超时. 因为这道题有多个营救者,所以我们从被营救者开始bfs,找到最近的营救者就是最短时间. 先定义一个结构体,存放坐标x和y,还有到达当前点(x,y)消耗的时间. struct node { int x,y; int time; friend bool operator < (const node &a,const node &

hdu 2102 A计划 详细题解 (BFS+优先队列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 开始看到四分之一的AC率感觉有点吓人,后来一做感觉就是模板改了点东西而已,一遍就AC了,不过在主函数和全局变量里面都定义了n和m导致我白白浪费了debug的时间.果然全局变量得小心用啊. 跟模板一样的,定义一个结构体,只不过多加了个参数,就是迷宫的层数,我用0代表第一层,1代表第二层,这在数组里面会体现的. struct node { int index;//层数

hdu 3345 War Chess (bfs+优先队列)

War Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1732    Accepted Submission(s): 416 Problem Description War chess is hh's favorite game: In this game, there is an N * M battle map, an

hdu 2102 A计划 具体题解 (BFS+优先队列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感觉有点吓人,后来一做感觉就是模板改了点东西而已,一遍就AC了,只是在主函数和全局变量里面都定义了n和m导致我白白浪费了debug的时间. 果然全局变量得小心用啊. 跟模板一样的,定义一个结构体,仅仅只是多加了个參数,就是迷宫的层数,我用0代表第一层.1代表第二层,这在数组里面会体现的. struct node { int index;

HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 开始以为是水题,想敲一下练手的,后来发现并不是一个简单的搜索题,BFS做肯定出事...后来发现题目里面也有坑 题意是从r到a的最短距离,"."相当时间单位1,"x"相当时间单位2,求最短时间 HDU 搜索课件上说,这题和HDU1010相似,刚开始并没有觉得像剪枝,就改用  双向BFS   0ms  一Y,爽! 网上查了一下,神牛们竟然用BFS+

HDU 1242 -Rescue (双向BFS)&amp;amp;&amp;amp;( BFS+优先队列)

题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出事...后来发现题目里面也有坑 题意是从r到a的最短距离,"."相当时间单位1,"x"相当时间单位2,求最短时间 HDU 搜索课件上说,这题和HDU1010相似,刚開始并没有认为像剪枝,就改用  双向BFS   0ms  一Y,爽! 网上查了一下,神牛们居然用BFS+优