Hdu2437-Jerboas(取余数判重搜索)

  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
 
题意:简单点说就是有N个点M条边,有些点是T(temporary  burrow),有些点是P(permanent burrow).给出起点,要找到一个P点,而且路径长度恰好是K的倍数。
尽量找路径长度短的,如果有两个路径长度相同的,则取字典序小的。
 
解析:优先队列,总是先取出路径长度小的,因为K最大只有1000,取余数,用vis[余数][点的编号]保存状态,为甚么这样标记是正确的,因为优先队列就已经保证先被
标记的状态路径长度就已经是最小的了。
 
代码
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<utility>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iterator>
#include<stack>
using namespace std;
const int INF=1e9+7;
const double eps=1e-7;
const int maxn=1005;
int N,M,start,mod;
bool vis[maxn][maxn];
char road[maxn];
struct node
{
    int val,rest,pos;
    node(int val=0,int rest=0,int pos=0):val(val),rest(rest),pos(pos){}
    bool operator < (const node& t) const{ return val>t.val; }
};
priority_queue<node> que;
struct edge
{
    int u,v,c;
    edge(int u=0,int v=0,int c=0):u(u),v(v),c(c){}
};
vector<edge> G[maxn];
void init()
{
    while(!que.empty()) que.pop();
    for(int i=0;i<maxn;i++) G[i].clear();
    memset(vis,false,sizeof(vis));
}
void solve()
{
    int ansl=INF,ansp=-1;
    que.push(node(0,0,start));
    vis[0][start]=true;  
    while(!que.empty())
    {
        node t=que.top();  que.pop();
        int val=t.val,rest=t.rest,pos=t.pos;
        if(val>ansl) break; //路径长度大了
        if(rest==0&&road[pos]=='P')  //是P点更新答案
        {
            if(val<ansl||(val==ansl&&pos<ansp))
            {
                ansl=val;
                ansp=pos;
            }
        }
        int Size=G[pos].size();
        for(int i=0;i<Size;i++)
        {
            edge& e=G[pos][i];
            int v=e.v,c=e.c+val;
            if(vis[c%mod][v]) continue;
            vis[c%mod][v]=true;
            que.push(node(c,c%mod,v));
        }
    }
    if(ansp==-1) printf("-1 -1
");
    else printf("%d %d
",ansl,ansp);
}
int main()
{
    int T,Case=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d%d",&N,&M,&start,&mod);
        scanf("%s",road+1);   //输入
        init();
        int u,v,c;
        while(M--)
        {
            scanf("%d%d%d",&u,&v,&c);
            G[u].push_back(edge(u,v,c));//
        }
        printf("Case %d: ",++Case);
        solve();
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/wust-ouyangli/p/5660355.html