(HDU2437) Jerboas-DFS-余数判重

Jerboas

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2030    Accepted Submission(s): 558


 

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

 

题目分析:

要从出发地到达指定地点且长度要是K的整数倍。没什么难的就是dfs,有问题的地方可能就是判重上。这里的判重用二维数组来表示,第一维代表到达的点第二维为出发地到目的地的长度模K得到的余数。

代码:https://blog.csdn.net/watermuch/article/details/9619471

#include<iostream>
#include<algorithm>
#define MAX 1111
#define INF 0x7FFFFFFF
using namespace std;
int head[MAX], num = 0, n, m, k, st;
int dp[MAX], vis[MAX][MAX];
char a[MAX];
struct node {
	int s, e, v, next;
}ed[22222];
void init() {
	memset(vis, 0, sizeof(vis));
	memset(head, -1, sizeof(head));
	num = 0;
}

void add(int s, int e, int v) {
	ed[num].s = s;
	ed[num].e = e;
	ed[num].v = v;
	ed[num].next = head[s];
	head[s] = num++;
}

int dfs(int v0, int u0, int cur)
{
	//到达要到达的目的地,并且长度满足条件
	if (a[v0 - 1] == 'P'&&cur%k == 0)return dp[v0] = min(dp[v0], cur);
	for (int i = head[v0]; i != -1; i = ed[i].next)
	{
		int e = ed[i].e;
		int v = ed[i].v + cur;
		//以当前节点为终点的,到当前节点的总长度为v没有被访问过,并且长度比原来保存的小
		if (vis[e][v%k] == 0 || v < vis[e][v%k])
		{
			vis[e][v%k] = v;
			dfs(e, u0, v);
		}
	}
}
int main() {
	int T;
	cin >> T;
	int casee = 1;
	while (T--)
	{
		init();
		cin >> n >> m >> st >> k;
		for (int i = 1; i <= n; i++)
			dp[i] = INF;
		getchar();
		int aa, bb, cc;
		cin >> a;
		for (int i = 1; i <= m; i++)
		{
			cin >> aa >> bb >> cc;
			add(aa, bb, cc);
		}
		cout << "Case " << casee++ << ": ";
		dfs(st, st, 0);
		int flag = 0, ind, ans = INF;
		for (int i = 1; i <= n; i++)
		{
			if (dp[i] != INF)
			{
				flag = 1;
				if (ans > dp[i]) {
					ans = dp[i];
					ind = i;
				}
			}
		}
		if (flag)cout << ans << " " << ind << endl;
		else cout << "-1 -1" << endl;
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值