• pat 甲级 1003 Emergency(最短路计数)


    1003 Emergency (25 分)

    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 Specification:

    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 N1), 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 Specification:

    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
    题目大意:n个城市,m条路,现在要从c1城市到达c2城市
    城市编号从0——n-1;
    每个城市各有一些应急通道
    求从c1到c2的最短路径有几条,以及在这些最短路中的应急通道最多有多少条;

    思路:求最短路的数量;
    手写dfs
    代码如下:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int n,m,c1,c2;
     4 int num[505],dis[505][505];
     5 int vis[505];
     6 int minn = 0x3f3f3f3f;
     7 int count1 = 1,maxn = 0;
     8 void dfs(int ci,int length,int rod)
     9 {
    10     if(ci == c2)//到达目的地 
    11     {
    12         if(length < minn)//路程更优 
    13         {
    14             minn = length;//更新最短路 
    15             count1 = 1;//更新最短路数量 
    16             maxn = rod;//更新最多的急救线路 
    17         }
    18         else if(length == minn)//又找到一条最短路 
    19         {
    20             count1 ++;//更新最短路数量 
    21             maxn = max(maxn,rod);
    22         }
    23         else
    24         return ;
    25     }
    26     if(length > minn)
    27     return ;
    28     for(int i = 0;i < n;i++)
    29     {
    30         if(vis[i] || dis[ci][i] == -1)
    31         //如果此城市已被访问过,或与起点之间没有路径 
    32             continue;
    33         vis[i] = 1;
    34         dfs(i,length + dis[ci][i],rod + num[i]);
    35         vis[i] = 0; 
    36     }
    37 }
    38 int main()
    39 {
    40     int a,b,l;
    41     while(cin >> n >> m >> c1 >> c2)
    42     {
    43         memset(num,0,sizeof(num));
    44         memset(dis,-1,sizeof(dis));
    45         memset(vis,0,sizeof(vis));
    46         for(int i = 0;i < n;i++)
    47             cin >> num[i];
    48         for(int i = 0;i < m;i++)
    49         {
    50             cin >> a >> b >> l;
    51             dis[a][b] = dis[b][a] = l;
    52         }
    53         dfs(c1,0,num[c1]);
    54         cout << count1 << " " << maxn << endl;
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    poj3537--Crosses and Crosses
    poj3480--John
    poj2975--Nim
    poj2960 S-Nim
    poj2505-A multplication game
    Team Queue(POJ 2259)
    将caffemodel文件转换为Matlab可用的数据形式
    Invalid MEX-file: caffe.mexa64 的解决方案
    mongoDB-3.x启用认证
    mongoDB跨平台图形管理工具
  • 原文地址:https://www.cnblogs.com/lu1nacy/p/9999116.html
Copyright © 2020-2023  润新知