zoukankan      html  css  js  c++  java
  • 1003. Emergency (25)

    原题连接:https://www.patest.cn/contests/pat-a-practise/1003

    题目如下:

    As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

    Input

    Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

    Output

    For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
    All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

    Sample Input
    5 6 0 2
    1 2 1 5 3
    0 1 1
    0 2 2
    0 3 1
    1 2 1
    2 4 1
    3 4 1
    
    Sample Output
    2 4
    
    这道题一开始我并没有想到用Dijkra,只是想到了利用递归,但是有好几个case都过不去。代码如下:
     1 #include<stdio.h>
     2 #define MaxCity 501
     3 
     4 int MostTeams,ShortestL,Teams,Length,Amount;
     5 int G[MaxCity]={0};
     6 int R[MaxCity][MaxCity];
     7 int Already[MaxCity][MaxCity]={0};
     8 int N,M,C1,C2;
     9 
    10 void DFS(int Last,int In)
    11 {
    12     int i;
    13     Teams+=G[Last];
    14     Length+=R[Last][In];
    15     Already[Last][In]=Already[In][Last]=1;
    16 
    17     if (In==C2)
    18     {
    19         Teams+=G[In];
    20         if(Length<ShortestL)
    21         {
    22             Amount=1;
    23             ShortestL=Length;
    24             MostTeams=Teams;
    25         }
    26         else if (Length==ShortestL)
    27         {
    28             Amount++;
    29             if (MostTeams<Teams)MostTeams=Teams;
    30         }
    31         else if (Length>ShortestL)
    32         {
    33             Length-=R[Last][In];
    34             return ;
    35         }
    36     }
    37 
    38     for (i=0;i<N;i++)
    39     {
    40         if (i!=C1 && R[In][i]!=-1 && (Already[In][i]==0 || Already[i][In]==0 ))DFS(In,i);
    41     }
    42 
    43    // return ;
    44 }
    45 
    46 int main()
    47 {
    48     int i,j,v,w,len;
    49     MostTeams=Teams=Length=Amount=0;
    50     ShortestL=0x7FFFFF;
    51     for (i=0;i<MaxCity;i++)
    52     {
    53         for (j=0;j<MaxCity;j++)R[i][j]=R[j][i]=-1;
    54     }
    55     scanf("%d %d %d %d",&N,&M,&C1,&C2);
    56     for (i=0;i<N;i++)
    57     {
    58         scanf("%d",&G[i]);
    59     }
    60     for (i=0;i<M;i++)
    61     {
    62         scanf("%d %d %d",&v, &w, &len);
    63         R[v][w]=R[w][v]=len;
    64     }
    65 
    66     if (C1==C2){printf("%d %d",1,G[C1]);}
    67     else {
    68     for (i=0;i<N;i++)
    69     {
    70         for (v=0;v<N;v++)
    71         {
    72             for (w=0;w<N;w++)Already[v][w]=Already[w][v]=0;
    73         }
    74         Teams=0;Length=0;
    75         if (R[C1][i]!=-1)DFS(C1,i);
    76     }
    77 
    78     printf("%d %d",Amount,MostTeams);
    79     }
    80 
    81     return 0;
    82 }
    View Code

    看了其他人的解法,发现核心还是最短路径,不管有没有使用递归,于是,用了其他人的样例,终于知道在递归的条件判断中,点是否在最短路径上这一条件及其重要!

    几个备用的测试样例:

    4 5 0 3                                    
    1 4 1 2
    0 1 1
    0 2 2
    0 3 4
    1 2 1
    2 3 2
      

    ——————

    6 9 0 2

    1 2 1 5 3 4

    0 1 1

    0 2 2

    0 3 1

    1 2 1

    2 3 1

    2 4 1

    2 5 1

    3 4 1

    4 5 1

    AC的解法:

      1 #include<stdio.h>
      2 #define INFINITY 65535
      3 #define MaxCity 503
      4 int N,M,C1,C2;
      5 int Team[MaxCity];
      6 int G[MaxCity][MaxCity];
      7 int collect[MaxCity]={0};
      8 int dist[MaxCity];
      9 int NumberR=0,NumberP=0;
     10 
     11 void init();
     12 int FindMin();
     13 void Dijkstra();
     14 void DFS(int,int );
     15 
     16 int main()
     17 {
     18     scanf("%d %d %d %d",&N,&M,&C1,&C2);
     19     int i;
     20     for (i=0;i<N;i++)
     21     {
     22         scanf("%d",&Team[i]);
     23     }
     24 
     25     init();
     26     Dijkstra();
     27 
     28     for (i=0;i<N;i++)collect[i]=0;
     29     collect[C1]=1;
     30 
     31     DFS(C1,Team[C1]);
     32     printf("%d %d",NumberR,NumberP);
     33     return 0;
     34 }
     35 
     36 void init()
     37 {
     38     int i,j,v,w,len;
     39     for (i=0;i<N;i++)
     40     {
     41         for (j=0;j<N;j++)
     42         {
     43             if (i==j)G[i][j]=G[j][i]=0;
     44             else G[i][j]=G[j][i]=INFINITY;
     45         }
     46         dist[i]=INFINITY;
     47     }
     48     for (i=0;i<M;i++)
     49     {
     50         scanf("%d %d %d",&v,&w,&len);
     51         G[v][w]=G[w][v]=len;
     52     }
     53 }
     54 
     55 int FindMin()
     56 {
     57     int MinV,v;
     58     int MinDist=INFINITY;
     59     for (v=0;v<N;v++)
     60     {
     61         if (!collect[v] && dist[v]<MinDist)
     62         {
     63             MinV=v;
     64             MinDist=dist[v];
     65         }
     66     }
     67     if (MinDist<INFINITY)
     68     {
     69         return MinV;
     70     }
     71     else return -1;
     72 }
     73 
     74 void Dijkstra()
     75 {
     76     int v,i;
     77 
     78     collect[C1]=1;
     79     dist[C1]=0;
     80 
     81     for (v=0;v<N;v++)
     82     {
     83         if (!collect[v] && G[C1][v]<INFINITY)dist[v]=G[C1][v];
     84     }
     85 
     86     while(1)
     87     {
     88         v=FindMin();
     89         if (v==-1)break;
     90         collect[v]=1;
     91         for (i=0;i<N;i++)
     92         {
     93             if (!collect[i] && dist[i]>dist[v]+G[v][i])
     94             {
     95                 dist[i]=dist[v]+G[v][i];
     96             }
     97         }
     98     }
     99 }
    100 
    101 void DFS(int src,int Numberp)
    102 {
    103     if (src==C2)
    104     {
    105         if (Numberp>NumberP)NumberP=Numberp;
    106         NumberR++;
    107         return ;
    108     }
    109 
    110     int i;
    111     for (i=0;i<N;i++)
    112     {
    113         if (collect[i]==0 && (dist[i]==dist[src]+G[src][i]))
    114         {
    115             collect[i]=1;
    116             DFS(i,Numberp+Team[i]);
    117             collect[i]=0;
    118         }
    119     }
    120 }
    View Code
  • 相关阅读:
    c#读取.config文件内容
    c# 读取配置文件方法
    C# Log4net详细说明
    C# 运算符集
    LeetCode 69_ x 的平方根
    LeetCode 172 _ 阶乘后的零
    LeetCode 171 _ Excel表列序号
    LeetCode 88 _ 合并两个有序数组
    LeetCode 581 _ 最短无序连续子数组
    LeetCode 283 _ 移动零
  • 原文地址:https://www.cnblogs.com/wuxiaotianC/p/6298585.html
Copyright © 2011-2022 走看看