您的位置:首页 > 其它

hdu 2437 Jerboas(bfs,dfs)

2015-08-15 14:49 405 查看
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


题意:给定一幅图,图上有两种点T,P.......一只跳鼠在一个T点作为起始点(s),它想通过图上的路到达某个P点,P点满足如下要求:

(1).到达P点的途中路径权值为k的倍数

(2).尽量让路径权值取最小

(3).权值相同时,P点取更小的

BFS+优先队列。因为要求最小值,出队的时候必须保证最小的先出队,要用优先队列来维护。入队的时候让比当前状态距离小的入队。不能简单的标记走没有走过。

DFS:先用的BFS做的,后来看到网上基本都是DFS,其实思想都是差不多的,就是样子变了而已。

bfs:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
#define INF 999999999
#define maxn 1000
int n,m,s,k;
struct node
{
int v,w;
int next;
}Edge[maxn*21];
int head[maxn+5],cnt;
char str[maxn+5];
long long vis[maxn+5][maxn+5];
void Add(int u,int v,int w)
{
cnt++;
Edge[cnt].v=v;
Edge[cnt].w=w;
Edge[cnt].next=head[u];
head[u]=cnt;
}
long long ans;
int ansu;
struct node2
{
long long ans;
int u;
node2()
{
this->ans=0;
u=0;
}
bool operator<(const node2 &a)const
{
return ans>a.ans;
}
};
void bfs(int u)
{
priority_queue<node2>q;
node2 a,temp;
a.u=u;
q.push(a);
ans=ansu=INF;
vis[u][0]=1;
while (!q.empty())
{
a=q.top();
q.pop();
if(a.ans && str[a.u]=='P' && a.ans%k==0 && a.ans<=ans)
{
ans=a.ans;
if(ansu>a.u)
ansu=a.u;
}
for (int i=head[a.u]; i!=-1; i=Edge[i].next)
{
temp=a;
temp.ans+=Edge[i].w;
int v=Edge[i].v;
if(!vis[v][temp.ans%k] || temp.ans<vis[v][temp.ans%k])
{
vis[v][temp.ans%k]=temp.ans;
temp.u=v;
q.push(temp);
}
}
}
if(ans==INF)
printf("-1 -1\n");
else
printf("%lld %d\n",ans,ansu);
}
int main()
{
int t,u,v,w;
scanf("%d",&t);
for(int j=1;j<=t;j++)
{
scanf("%d%d%d%d",&n,&m,&s,&k);
getchar();
scanf("%s",str+1);
memset(head, -1, sizeof(head));
memset(vis, 0, sizeof(vis));
cnt=0;
for (int i=0; i<m; i++)
{
scanf("%d%d%d",&u,&v,&w);
Add(u, v, w);
}
printf("Case %d: ",j);
bfs(s);
}
return 0;
}


dfs:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
#define INF 999999999
#define maxn 1000
int n,m,s,k;
struct node
{
int v,w;
int next;
}Edge[maxn*21];
int head[maxn+5],cnt;
char str[maxn+5];
long long vis[maxn+5][maxn+5];
void Add(int u,int v,int w)
{
cnt++;
Edge[cnt].v=v;
Edge[cnt].w=w;
Edge[cnt].next=head[u];
head[u]=cnt;
}
long long ans;
int ansu;
void dfs(int u,int val)
{
if(val>ans && ans!=-1)
return;
if(str[u]=='P' && val%k==0)
{
if(val<ans || ans==-1)
{
ans=val;
ansu=u;
return;
}
else if(val==ans && u<ansu)
ansu=u;
return;
}
for (int i=head[u]; i!=-1; i=Edge[i].next)
{
int v=Edge[i].v;
val+=Edge[i].w;
if(!vis[v][val%k] || val<vis[v][val%k])
{
vis[v][val%k]=val;
dfs(v, val);
val-=Edge[i].w;
}
}
}

int main()
{
int t,u,v,w;
scanf("%d",&t);
for(int j=1;j<=t;j++)
{
scanf("%d%d%d%d",&n,&m,&s,&k);
getchar();
scanf("%s",str+1);
memset(head, -1, sizeof(head));
memset(vis, 0, sizeof(vis));
cnt=0;
ans=ansu=-1;
for (int i=0; i<m; i++)
{
scanf("%d%d%d",&u,&v,&w);
Add(u, v, w);
}
dfs(s, 0);
printf("Case %d: %lld %d\n", j, ans, ansu);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: