zoukankan      html  css  js  c++  java
  • 1003 Emergency

    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 (≤) - 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
    
     
    题意以及思路:这道题跟天梯赛 L2-001 紧急救援 差不多,题意可以说是一样的,唯一一点不同就是这题不需要输出最短路同时最大救援队的路径,所以显然这题要简单一点,我这里是直接用的dfs暴力搜索,具体写法可以看看注释,写的很清楚,由于数据不算大,根本不存在超时的问题,当然用dijkstra算法会更好一点,具体写法有空再补......
     
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<map>
     7 #include<set>
     8 #include<vector>
     9 using namespace std;
    10 #define ll long long 
    11 #define dd cout<<endl
    12 const int inf=99999999;
    13 const int mod=1e9+7;
    14 const int maxn=505;
    15 int book[maxn]={0};
    16 int maxx=0;//记录最大救援队数目 
    17 int countt=1;//记录最短路径条数 
    18 int minn=inf;//记录最短路径长度 
    19 int n,m,startt,endd;
    20 int help[maxn];//每个城市救援队数目 
    21 int x,y,z;
    22 int e[maxn][maxn];//地图 
    23 void dfs(int cur,int dis,int sum_help)
    24 {
    25     if(dis>minn)//比原来最短路径还要长,直接返回 
    26         return ;
    27     if(cur==endd)//到达终点 
    28     {
    29         if(dis==minn)
    30         {
    31             countt++;//最短路径相同,条数加一 
    32             if(sum_help>maxx)
    33                 maxx=sum_help;//刷新救援队 
    34          } 
    35         else if(dis<minn)
    36         {
    37             minn=dis;//刷新最短路以及条数,以及救援队 
    38             countt=1; 
    39             maxx=sum_help; //救援队最大值记录为当前数目 
    40         }
    41         return ;//当前已经达到终点,所以也要返回 
    42     }
    43     for(int i=0;i<n;i++)//开始搜索 
    44     {
    45         if(e[cur][i]!=inf&&book[i]==0)//注意要符合条件 
    46         {
    47              book[i]=1;//标记
    48              dfs(i,dis+e[cur][i],sum_help+help[i]);
    49              book[i]=0;//回溯 
    50         }
    51     }
    52     return ; 
    53 }
    54 int main()
    55 {
    56     //n个城市,m条边,起点,终点(注意城市从0开始,不是从一开始) 
    57     scanf("%d%d%d%d",&n,&m,&startt,&endd);
    58     //输入救援队 
    59     for(int i=0;i<n;i++)
    60         scanf("%d",&help[i]);
    61     //地图初始化 
    62     for(int i=0;i<n;i++)
    63         for(int j=0;j<n;j++)
    64             i==j?e[i][j]=0:e[i][j]=inf;
    65     //读入边
    66     for(int i=0;i<m;i++)
    67     {
    68         scanf("%d%d%d",&x,&y,&z);
    69         e[x][y]=z;//无向图
    70         e[y][x]=z;//双向存边 
    71     } 
    72     book[startt]=1;//标记起点 
    73     dfs(startt,0,help[startt]);//0表示距离,0之后的数表示救援队数量 
    74     printf("%d %d
    ",countt,maxx);
    75     return 0;
    76 }
    大佬见笑,,
  • 相关阅读:
    返回顶部,跳到底部
    [Swift]LeetCode1068.
    [Swift]LeetCode1067. 范围内的数字计数 | Digit Count in Range
    [Swift]LeetCode1066. 校园自行车分配 II | Campus Bikes II
    [Swift]LeetCode1065. 字符串的索引对 | Index Pairs of a String
    [Swift]LeetCode1064. 不动点 | Fixed Point
    [Algorithm]扔杯问题
    CleanWipe:无需密码彻底卸载Symantec(赛门铁克)
    [Swift]获取UIColor的HSV/HSB值(Hue色相、S饱和度、B亮度)
    [Swift]LeetCode1050.合作至少三次的演员和导演 | Actors and Directors Who Cooperated At Least Three Times
  • 原文地址:https://www.cnblogs.com/xwl3109377858/p/10660640.html
Copyright © 2011-2022 走看看