1003. Emergency (25)
来源:互联网 发布:布谷鸟网络电视回看 编辑:程序博客网 时间:2024/06/11 00:46
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 Input5 6 0 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1Sample Output2 4
图使用链式存储。算法采用回溯+穷举,没有使用DFS或者Dijkstra算法
#include <stdio.h>#include <stdlib.h>#define bool inttypedef struct ROAD{int c1;int c2;int L;struct ROAD *next;}ROAD;typedef struct HEAD_TABLE_NODE{ROAD* head;ROAD* cur;}HEAD_TABLE_NODE;void insertNode(HEAD_TABLE_NODE *headsTable,int c1,ROAD *r){if(headsTable[c1].head == NULL){headsTable[c1].head = r;headsTable[c1].cur = r;}else{headsTable[c1].cur->next = r;headsTable[c1].cur = r;}}int main(){int N,M,C1,C2;int c1,c2,l,start;int resSum = 0,maxSum = 0;int pathCnt = 0;int pathL = 0,minL = 1000000;int *teams;int des = 0;int stack[1000],indexOfStack;HEAD_TABLE_NODE *headsTable;int i;ROAD *r;ROAD *tmp;bool visited[1000] = {0};scanf("%d%d%d%d",&N,&M,&C1,&C2);teams = (int *)malloc(N*sizeof(int));headsTable = (HEAD_TABLE_NODE *)malloc(N*sizeof(HEAD_TABLE_NODE));for(i = 0;i < N;++i){scanf("%d",&teams[i]);}for(i = 0;i < N;++i){headsTable[i].head = NULL;headsTable[i].cur = NULL;}for(i = 0;i < M;++i){scanf("%d%d%d",&c1,&c2,&l);r = (ROAD *)malloc(sizeof(ROAD));r->c1 = c1;r->c2 = c2;r->L = l;r->next = NULL;insertNode(headsTable,c1,r);r = (ROAD *)malloc(sizeof(ROAD));r->c1 = c2;r->c2 = c1;r->L = l;r->next = NULL;insertNode(headsTable,c2,r);}if(C1 != C2){for(i = 0;i < N;++i){headsTable[i].cur = headsTable[i].head;}start = C1;visited[start] = 1;resSum = teams[start];indexOfStack = 0;while(headsTable[C1].cur != NULL){while(headsTable[start].cur != NULL){des = headsTable[start].cur->c2;if(visited[des]){headsTable[start].cur = headsTable[start].cur->next;continue;}visited[des] = 1;resSum = resSum + teams[des];pathL = pathL + headsTable[start].cur->L;if(des == C2) {if(pathL < minL){minL = pathL;pathCnt = 1;maxSum = resSum;}else if(pathL == minL){pathCnt++;if(maxSum < resSum)maxSum = resSum;}resSum = resSum - teams[des];pathL = pathL - headsTable[start].cur->L;visited[des] = 0;headsTable[start].cur = headsTable[start].cur->next;}else{stack[indexOfStack++] = start;start = des;}}if(headsTable[start].cur == NULL && start != C1){headsTable[start].cur = headsTable[start].head;start = stack[--indexOfStack];des = headsTable[start].cur->c2;resSum = resSum - teams[des];pathL = pathL - headsTable[start].cur->L;headsTable[start].cur = headsTable[start].cur->next;for(i = 0;i < N;++i) visited[i] = 0;for(i = 0;i <= indexOfStack;++i) visited[stack[i]] = 1;}}}else{pathCnt = 1;maxSum = teams[C1];}printf("%d %d",pathCnt,maxSum);free(teams);for(i = 0;i < N;++i){headsTable[i].cur = headsTable[i].head;while(headsTable[i].cur != NULL){tmp = headsTable[i].cur;headsTable[i].cur = tmp->next;free(tmp);}}free(headsTable);return 0;}
- 1003. Emergency (25)
- 1003. Emergency (25)-PAT
- (PAT)1003. Emergency (25)
- 1003. Emergency (25)
- 【C++】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)
- 1003. Emergency (25)
- 1003. Emergency (25)
- 1003. Emergency (25)
- RTutor Kicks Off: An Interactive R Problem Set about Top Income Taxation
- 台球碰撞
- leetcode Binary Tree Preorder
- yii 安全问题
- V4L2用户空间和kernel层driver的交互过程
- 1003. Emergency (25)
- Unity中加入虚拟按钮不可见
- ACM--steps--悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(多重背包)
- POJ 3311 Hie with the Pie (Floyd + 状压dp 简单TSP问题)
- php开发app接口之静态缓存
- leetcode:two sum 菜鸟解法
- 十四
- Chrome插件开发学习心得(四)之修改浏览器菜单
- java 串行化学习