码迷,mamicode.com
首页 > 其他好文 > 详细

1003 Emergency

时间:2019-04-06 09:32:12      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:fonts   extra   最大   integer   紧急   lis   start   const   一点   

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, C?1?? and C?2?? - 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 c?1??, c?2?? 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 C?1?? to C?2??.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C?1?? and C?2??, 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\n",countt,maxx);
75     return 0;
76 }

 

 

1003 Emergency

标签:fonts   extra   最大   integer   紧急   lis   start   const   一点   

原文地址:https://www.cnblogs.com/xwl3109377858/p/10660640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!