hdu 2437 Jerboas (建图+bfs+取余判重)

Jerboas

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


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


题意:
给定一幅图,图上有两种点T,P.......一只跳鼠在一个T点作为起始点,它想通过图上的路到达某个P点,P点满足如下要求:
(1).到达P点的途中路径权值为k的倍数
(2).尽量让路径权值取最小
(3).权值相同时,P点取更小的


思路:
此题可不是最短路哦,设总距离为dist,因为他可以重新走一些路使得dist为k的倍数。正确的方法当然是bfs啦,用vis[i][dist%k]判重就行了,满足y最小可以用优先队列实现,不过要注意的是第一次进队列的相同状态中不一定是最优的,可以参考我贴的数据,最后满足z最小就是搜到第一个答案不要急着return就够了。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 40005
#define OO (1<<31)-1
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-12
typedef long long ll;
using namespace std;

int n,m,ans,cnt,sx,k,res;
int vis[maxn][maxn],ok[maxn];
char s[maxn];
int p[maxn];
struct Node
{
    int v,w,next;
}edge[MAXN];
struct node
{
    int nx,dist,mod;
    bool operator <(const node& xx)const
    {
        return dist>xx.dist;
    }
}cur,now;

void addedge(int u,int v,int w)
{
    cnt++;
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=p[u];
    p[u]=cnt;
}
bool bfs()
{
    int i,j,t,nx,tx,v,w,tdist,tmod;
    priority_queue<node>q;
    ans=INF;
    memset(vis,-1,sizeof(vis));
    vis[sx][0]=0;
    cur.dist=0; cur.mod=0; cur.nx=sx;
    q.push(cur);
    while(!q.empty())
    {
        now=q.top();
        q.pop();
 //       printf("nx:%d mod:%d dist:%d\n",now.nx,now.mod,now.dist);
        nx=now.nx;
        if(ans<INF&&now.dist>ans) return true ;
        if(ok[nx]&&now.mod==0)
        {
            if(ans>now.dist)
            {
                ans=now.dist;
                res=nx;
            }
            else if(ans==now.dist)
            {
                res=min(res,nx);
            }
        }
        for(i=p[nx];i;i=edge[i].next)
        {
            v=edge[i].v;
            w=edge[i].w;
            tmod=(now.mod+w)%k;
            tdist=now.dist+w;
            if(vis[v][tmod]==-1)
            {
                vis[v][tmod]=tdist;
                cur.dist=tdist;
                cur.mod=tmod;
                cur.nx=v;
                q.push(cur);
            }
            else if(vis[v][tmod]>tdist)
            {
                vis[v][tmod]=tdist;
                cur.dist=tdist;
                cur.mod=tmod;
                cur.nx=v;
                q.push(cur);
            }
        }
    }
    if(ans>=INF) return false ;
    return true ;
}
int main()
{
    int i,j,u,v,w,t,test=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d%s",&n,&m,&sx,&k,s+1);
        for(i=1;i<=n;i++)
        {
            if(s[i]=='T') ok[i]=0;
            else ok[i]=1;
        }
        memset(p,0,sizeof(p));
        cnt=0;
        for(i=1;i<=m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
        }
        printf("Case %d: ",++test);
        if(bfs()) printf("%d %d\n",ans,res);
        else printf("-1 -1\n");
    }
    return 0;
}
/*
2
5 6 1 7
TPPTP
1 2 8
1 4 7
4 3 9
2 3 6
1 5 3
4 3 14
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值