HDU-2437 Jerboas (DFS+同余剪枝)

HDU-2437

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.
这里写图片描述

  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.
  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
Source
2008 Asia Chengdu Regional Contest Online
题意:
给你n个点,有些点是临时停留点(T),有些点是永久停留点(P)
给你m条有向带权边,从A到B的距离是C
从S点出发,所走的路径长度为K的倍数并且结束点应为永久停留点
如果有多条路径,选择路径长度最短的
如果最短的有多条,选择结束点最小的
AC代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
using namespace std;
struct st
{
    int y,d;
}kk;
vector<st>ve[1005];//使用vector和结构体存图
                    //有人说这样存图超时,需要用vector嵌套
int dis[1005][1005];//同余剪枝标记数组
                    //dis[i][j]表示到达i点路径长度对k取余为j的最小值
int n,m,s,k;
string ss;//表示点的性质的字符串
int y,z;//y存储最小距离,z存储最小点
void dfs(int pos,int ans)//当前位置,当前路径长度
{
    if(ans%k==0&&ss[pos-1]=='P'&&(ans<y||(ans==y&&pos<z))){
    //当前路径长度整除k,当前点是永久点,路径长度短或者长度相同点小
        z=pos;
        y=ans;
        return ;
    }
    for(int i=0;i<ve[pos].size();i++){
        int y=ve[pos][i].y;
        int d=ve[pos][i].d+ans;
        if(dis[y][d%k]==-1||dis[y][d%k]>d){//同余剪枝,否则TLE
        //只有当到达下一个点路径长度对k取余的这个路径长度小于原来的才被更新深搜
            dis[y][d%k]=d;
            dfs(y,d);
        }
    }
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    int ci=1;
    cin>>t;
    while(t--){
        cin>>n>>m>>s>>k;
        cin>>ss;
        int a,b,c;
        for(int i=0;i<m;i++){
            cin>>a>>b>>c;
            //存储有向带权图
            kk.y=b;
            kk.d=c;
            ve[a].push_back(kk);
        }
        y=999999999;
        z=0;
        memset(dis,-1,sizeof(dis));//初始化
        dfs(s,0);
        if(y==999999999) cout<<"Case "<<ci++<<": -1 -1"<<endl;
        else cout<<"Case "<<ci++<<": "<<y<<" "<<z<<endl;
        for(int i=0;i<n;i++)
            ve[i].clear();//清空vector,否则会MLE
    }
return 0;}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值