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;
}                                 


Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值