zoukankan      html  css  js  c++  java
  • Jerboas 邻接表+DFS

        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
    #include <iostream>
    #include <vector>
    #define MAX 1002
    using namespace std;
    bool ok[MAX],flag;
    int n,m,t,kk,start;
    int MM,bestend;
    typedef struct node
    {
        int dis;
        int vector;
        node(){}
        node(int vv,int dd)
        {
            vector=vv;
            dis=dd;
        }
    }Point;
    vector<Point>G[MAX];
    int edges[MAX][MAX];
    void Init()
    {    
        int i,j;
        scanf("%d%d%d%d",&n,&m,&start,&kk);
        char ch;
        for(i=1;i<=n;i++)
        {
            ok[i]=false;
            cin>>ch;
            if(ch=='P')
                ok[i]=true;
            G[i].clear();  
        }
        for(i=0;i<=n;i++)
            for(j=0;j<=n;j++)
                edges[i][j]=-1;
        int a,b,s;
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&s);
            G[a].push_back(node(b,s));
        }
    }
    void DFS(int ssum,int s)
    {
        int i;    
        if(ok[s]&&ssum%kk==0)
        {
            if(ssum<MM||MM==-1)
            {
                MM=ssum;
                bestend=s;
                flag=true;
            }
            else if(ssum==MM&&s<bestend)
                bestend=s;
            return ;
        }
        if(G[s].empty())
            return ;
        for(i=0;i<G[s].size();i++)
        {
            int t=ssum+G[s][i].dis;
            int next=G[s][i].vector;
            if(edges[next][t%kk]==-1||edges[next][t%kk]>t)
            {
                edges[next][t%kk]=t;
                DFS(t,next);
            }
        }
    }
    int main()
    {
        scanf("%d",&t);
        int www=1;
        while(t--)
        {
            Init();
            MM=-1;
            flag=false;
            DFS(0,start);
            printf("Case %d: ",www++);
            if(flag)
            {
                printf("%d %d\n",MM,bestend);
            }
            else
            {
                printf("-1 -1\n");
            }
        }
        return 0;
    }
  • 相关阅读:
    POJ3714+最近点对
    HDU1632+半平面交
    POJ2402+模拟
    ASP.NET MVC几种找不到资源的问题解决办法
    ASP.NET MVC中的错误-友好的处理方法
    ASP.NET MVC 程序 报错“CS0012: 类型“System.Data.Objects.DataClasses.EntityObject”在未被引用的程序集中定义”的解决办法
    【Reporting Services 报表开发】— 表达式
    【Reporting Services 报表开发】— 级联式参数设置
    【Reporting Services 报表开发】— 数据表的使用
    【Reporting Services 报表开发】— 矩阵的使用
  • 原文地址:https://www.cnblogs.com/xxxsans/p/12736333.html
Copyright © 2011-2022 走看看