1003. Emergency (25)[C语言]
来源:互联网 发布:淘宝直播怎么做起来 编辑:程序博客网 时间:2024/06/05 15:48
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
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.
5 6 0 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1Sample Output
2 4
#include <stdio.h>#include <stdlib.h>#include <limits.h>typedef struct Road{int c1 ;int c2 ;int length;int visited ;}road;static int min = INT_MAX ;static int maxhps = 0 ;static int nums = 0 ;static road rs[50005] ;staticint hs[50005] ;int getBorder(int cs[] ,int m , int c1){int i = 0, n = 0;for(i = 0 ; i < m ; i ++ ){if(rs[i].c1==c1 || rs[i].c2== c1){cs[n] = i;n++;}}return n;}void dfs (int m, int c1 , int c2,int sum,int maxhelps){int cs[m] ;if(c1 == c2 ){if(min > sum ){maxhps = maxhelps+hs[c1];nums=1;min = sum;return ;}else if(min == sum ){if(maxhelps+hs[c1]>maxhps){maxhps = maxhelps+hs[c1];}nums++; }return ;}int i = 0 ;int ns = getBorder(cs , m ,c1); for(i = 0 ; i < ns ; i ++ ){if(rs[cs[i]].visited == 1) continue;rs[cs[i]].visited = 1 ;maxhelps +=hs[c1];sum+=rs[cs[i]].length;if(rs[cs[i]].c1==c2 || rs[cs[i]].c2==c2 ){dfs(m,c2,c2,sum,maxhelps);}else if(rs[cs[i]].c1==c1 && rs[cs[i]].c2!=c2 ){dfs(m,rs[cs[i]].c2,c2,sum,maxhelps);}else if(rs[cs[i]].c2==c1 && rs[cs[i]].c1!=c2 ){dfs(m,rs[cs[i]].c1,c2,sum,maxhelps);}maxhelps -=hs[c1];sum-=rs[cs[i]].length;rs[cs[i]].visited = 0 ;}}int main(){int n , m , c1 , c2 ;int i ;scanf("%d" , &n);scanf("%d" , &m);scanf("%d" , &c1);scanf("%d" , &c2);for( i = 0 ; i < n;i++){scanf("%d" , &hs[i]);}for(i = 0 ; i < m ; i++){scanf("%d" , &rs[i].c1);scanf("%d" , &rs[i].c2);scanf("%d" , &rs[i].length);rs[i].visited = 0;}if(c1==c2){printf("1 %d" , hs[c1]);return 0;}dfs(m,c1,c2,0,0);printf("%d" , nums);printf(" %d" , maxhps);return 0;}
- 1003. Emergency (25)[C语言]
- PAT 1003. Emergency C语言
- 【C++】1003. Emergency (25)*
- 【PAT甲级】【C++】1003. Emergency (25)
- 1003. Emergency (25)
- 1003. Emergency (25)-PAT
- (PAT)1003. Emergency (25)
- 1003. Emergency (25)
- 1003. Emergency (25)
- PAT 1003. Emergency (25)
- 1003. Emergency (25)
- 1003. Emergency (25)
- PAT 1003. Emergency (25)
- 1003. Emergency (25)
- 1003. Emergency (25)
- [PAT]1003. Emergency (25)
- 1003. Emergency (25)
- 1003. Emergency (25)
- groovy进行数据库操作
- Java虚拟机之内存区域与内存溢出异常总结
- 字符串区别于字符数组
- hadoop-cdh搭建
- 同类接口对象的传递
- 1003. Emergency (25)[C语言]
- PAT1053 住房空置率
- Java多线程-线程间的通信
- 多线程学习笔记4-线程间的通信(2)
- 元素的比较次数与元素初始排序无关的排序方法
- Python Notes (6)
- c/c++基础之一
- 从零开始学JAVA:从插入排序解析类执行过程(内存)
- 1004. Counting Leaves (30)[C语言]