HDU-2437-Jerboas(BFS+優先隊列)

來源:互聯網
上載者:User

標籤:des   style   http   os   io   for   ar   art   

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
25 5 1 7TPPTP1 2 81 4 74 3 92 3 61 5 35 5 1 7TPTTP1 2 81 4 74 3 92 3 61 5 3
 
Sample Output
Case 1: 14 3Case 2: -1 -1
 
Source2008 Asia Chengdu Regional Contest Online 


思路:BFS+優先隊列。因為要求最小值,出隊的時候必須保證最小的先出隊,要用優先隊列來維護。入隊的時候沒有簡單的判重,而是讓比目前狀態距離小的入隊。


#include <cstdio>#include <queue>#include <map>#define INF 999999999using namespace std;struct S{int pos;long long dis;bool operator<(const S & p) const{    return dis>p.dis;}}t;char mp[1005];int head[1005],nxt[20000],val[20000],des[20000],vis[1005][1000];int main(){    int T,n,m,s,k,i,j,u,v,w,casenum=1,disans,desans;    scanf("%d",&T);    while(T--)    {        scanf("%d%d%d%d",&n,&m,&s,&k);        scanf("%s",mp);        for(i=0;i<=n;i++) head[i]=-1;        for(i=0;i<m;i++)        {            scanf("%d%d%d",&u,&v,&w);            nxt[i]=head[u];            head[u]=i;            val[i]=w;            des[i]=v;        }        printf("Case %d: ",casenum++);        priority_queue<S>que;        for(i=1;i<=n;i++) for(j=0;j<k;j++) vis[i][j]=0;        vis[s][0]=1;        disans=desans=INF;        t.dis=0;        t.pos=s;        que.push(t);        while(!que.empty())        {            t=que.top();            que.pop();            if(t.dis && mp[t.pos-1]=='P' && t.dis%k==0 && t.dis<=disans)            {                disans=t.dis;                if(t.pos<desans) desans=t.pos;            }            for(i=head[t.pos];i!=-1;i=nxt[i])            {                t.dis+=val[i];                t.pos=des[i];                if(!vis[t.pos][t.dis%k] || t.dis<vis[t.pos][t.dis%k])                {                    vis[t.pos][t.dis%k]=t.dis;                    que.push(t);                }                t.dis-=val[i];            }        }        if(disans==INF) printf("-1 -1\n");        else printf("%d %d\n",disans,desans);    }}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.