2014
01-26

# Jerboas

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.

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)

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)

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

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

#include <iostream>
#include <cstring>
#include <cstdio>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN=1005;
const int MAXM=20005;
struct node
{
int u,v,w;
};
node edge[MAXM];
int first[MAXN],next[MAXM];
int cc,n,m,s,k;
inline void add_edge(int u,int v,int w)
{
edge[cc].u=u;
edge[cc].v=v;
edge[cc].w=w;
next[cc]=first[u];
first[u]=cc;
cc++;
}
char tag[MAXN];
int dis[MAXN][MAXN];
int res,z;
void dfs(int u,int cur_d)
{
int i;
if(tag[u]=='P'&&cur_d%k==0&&(cur_d<res||(cur_d==res&&u<z)))
{
res=cur_d;
z=u;
return ;
}
for(i=first[u];i!=-1;i=next[i])
{
int v=edge[i].v;
int d=cur_d+edge[i].w;
if(dis[v][d%k]==-1||dis[v][d%k]>d)
{
dis[v][d%k]=d;
dfs(v,d);
}

}
}
int main()
{
int t;
scanf("%d",&t);
int cas=0;
while(t--)
{
cas++;
scanf("%d%d%d%d",&n,&m,&s,&k);
int i;
getchar();
for(i=1;i<=n;i++)
scanf("%c",&tag[i]);
memset(first,-1,sizeof(first));
memset(next,-1,sizeof(next));
cc=0;
memset(dis,-1,sizeof(dis));
for(i=1;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
}
res=INF;
z=0;
dfs(s,0);
printf("Case %d:",cas);
if(res==INF)
printf(" -1 -1\n");
else
printf(" %d %d\n",res,z);
}
return 0;
}