hdu2437

Jerboas

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


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
 

Source
 

Recommend
lcy


 题意:
本题从起点出发到达任何一个‘P’点,求解满足到达的步数为k的倍数的尽量小的步数和到达的坐标位置。
 思路:
 运用bfs的思路即可。注意此处的状态不再单单只是坐标位置,还有到达的步数对k余数,两个数值一起构成当前的状态。

#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;

int n,m,s,k;
char ss[1007];

struct node
{
    int u,cost;
    node(){}
    node(int a,int b){u=a;cost=b;}

};
vector<node>ve[1007];
bool vis[1007][1007];

struct data
{
    int x;
    int cost;
    friend bool operator <(data a,data b)
    {
        if(a.cost==b.cost)
        {
            return a.x>b.x;
        }
        else
            return a.cost>b.cost;
    }
    data(){}
    data(int a,int b){x=a;cost=b;}
};

void inser(int a,int b,int c)
{
    ve[a].push_back(node(b,c));
}

priority_queue<data>que;
void bfs()
{
    memset(vis,0,sizeof vis);
    while(!que.empty())
        que.pop();

    vis[s][0]=1;
    que.push(data(s,0));

    while(!que.empty())
    {
        data now=que.top();
        que.pop();
        if(ss[now.x]=='P'&&now.cost%k==0)
        {
            printf("%d %d\n",now.cost,now.x);
            return ;

        }

        for(int i=0;i<ve[now.x].size();i++)
        {
            node path=ve[now.x][i];
            data ne=now;
            ne.x=path.u;
            ne.cost+=path.cost;
            if(vis[ne.x][ne.cost%k])
                continue;
            vis[ne.x][ne.cost%k]=1;
            que.push(ne);
        }

    }
    printf("-1 -1\n");

}
int main()
{
    int T;
    int cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d%d",&n,&m,&s,&k);
        getchar();
        scanf("%s",ss+1);
        for(int i=0;i<=n;i++)
            ve[i].clear();
        //printf("n:%d  m%d,s:%d  k:%d\n",n,m,s,k);
        for(int i=0;i<m;i++)
        {
           // printf("i:%d\n",i);
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            inser(a,b,c);

        }
        printf("Case %d: ",cas++);
        bfs();
    }
    return 0;
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值