Jerboas HDU - 2437

题目链接:Jerboas HDU - 2437

===================================================

Jerboas

Time Limit: 5000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)

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

===================================================

题意:就是一个老鼠,他有很多个洞,TP,临时和永久两种;给你一个洞与洞的单向路径图,而且没有自身到自身的路径;给出数字K,问从S洞开始,到任意一个永久洞的最短路,并且路径长度必须为K的倍数。

算法:优先队列+BFS

思路:

这里求最短路径,立马就想到BFS。

  1. 因为洞S题目已经给出了,初始洞一定为临时洞。
  2. 这里没有明确表示两个洞之间只有唯一路径,所以需要考虑两个洞存在不同长度的路径,使用二维vector存储。
  3. 判重关键点,路径长度和K倍数,所以只要求路径长度对K求余的余数就能判断,K最大为1000,所以判重数组,二维只要开到1000左右,即vis[点数量][1001]。
  4. 这里因为并不是简单判断最短路径,并且需要K倍数,对路径排序并没有用,所以需要优先队列,对入队的最短路进行优先出队。

我的出错点:这里我受到之前做题的影响,因为我上一道题Roll The CubeHDU - 3309 的bfs是步骤,每次只加一,所以把判断放在了入队前也没问题。然而对于现在这题来说不行。所以需要入队完,从队列取出来再判断,再加上是优先队列,每次取出来都是最短路,只要达到要求就是答案。

===================================================

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>

using namespace std;

int n,m,s,k;
char dong[1002];

struct guan{
    int e,f;
    guan(int ee,int ff):e(ee),f(ff) {};
};
vector<guan> v[1002];

bool vis[1002][1001];

void init(){
    cin>>n>>m>>s>>k;
    for(int i=1;i<=n;i++){
        char ch = getchar();while(ch=='\n' || ch==' ') ch = getchar();
        dong[i] = ch;
    }
    for(int i=1;i<=n;i++) v[i].clear();
    memset(vis,false,sizeof vis);
    for(int i=0;i<m;i++){
        int a,b,c;cin>>a>>b>>c;
        v[a].push_back(guan(b,c));
    }
}

struct node{
    int x,y;
    node(int xx,int yy):x(xx),y(yy) {};
    bool operator < (const node &a) const{
        if(y==a.y ) return x>a.x;
        return y>a.y;
    }
};

bool bfs(){
    priority_queue<node> q;
    vis[s][0] = true;q.push(node(s,0));
    while(!q.empty()){
        node p = q.top();q.pop();
        int x = p.x , y = p.y;
        if(dong[x]=='P'&&y%k==0){
                cout<<y<<" "<<x<<endl;
                return true;
            }
        for(int i=0;i<v[x].size();i++){
            int e = v[x][i].e , f = v[x][i].f + y;
            if(vis[e][f%k]) continue;
            vis[e][f%k] = true;
            q.push(node(e,f));
        }
    }
    return false;
}

int main()
{
    int _;cin>>_;
    for(int i=1;i<=_;i++){
        init();
        cout<<"Case "<<i<<": ";
        if(!bfs()) cout<<"-1 -1"<<endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盐太郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值