Sicily 1908. Jerboas

1908. Jerboas

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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.

Figure 5.10.1 Jerboa

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

// Problem#: 1908
// Submission#: 3590850
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <string.h>

const int MAXN = 1005;
const int MAXM = 20000;
const int MAXK = 1000;

int head[MAXN];
int to[MAXM];
int ptr[MAXM];
int len[MAXM];
int count[MAXN];
int queue[MAXN];
int f[MAXN][MAXK];
char stat[MAXN];
int n, m, s, k;

int main() {
    int cs;
    scanf("%d", &cs);
    for (int cn = 1; cn <= cs; cn++) {
        scanf("%d%d%d%d", &n, &m, &s, &k);
        scanf("%s", stat + 1);
        memset(head + 1, -1, sizeof(int) * n);
        memset(count + 1, 0, sizeof(int) * n);
        for (int j = 0; j < m; j++) {
            int a, b;
            scanf("%d%d%d", &a, &b, &len[j]);
            to[j] = b;
            ptr[j] = head[a];
            head[a] = j;
            count[b]++;
        }
        for (int i = 1; i <= n; i++) memset(f[i], -1, sizeof(int) * k);
        f[s][0] = 0;
        int t = 0;
        for (int i = 1; i <= n; i++) {
            if (count[i] == 0) queue[t++] = i;
        }
        for (int i = 0; i < t; i++) {
            int u = queue[i];
            for (int j = head[u]; j >= 0; j = ptr[j]) {
                int v = to[j];
                for (int r = 0; r < k; r++) {
                    if (f[u][r] < 0) continue;
                    int d = f[u][r] + len[j];
                    int r1 = d % k;
                    int & f1 = f[v][r1];
                    if (f1 < 0 || d < f1) f1 = d;;
                }
                count[v]--;
                if (count[v] == 0) queue[t++] = v;
            }
        }
        int z = 0;
        for (int i = 1; i <= n; i++) {
            int d = f[i][0];
            if (stat[i] == 'P' && d >= 0 && (z == 0 || d < f[z][0])) z = i;
        }
        if (z == 0) printf("Case %d: -1 -1\n", cn);
        else printf("Case %d: %d %d\n", cn, f[z][0], z);
    }
    return 0;
}                                 


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值