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 Input
1 2 3 4 5 6 7 8 |
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
1 |
2 4 |
深度优先搜索即可。
写的有点凌乱(⊙﹏⊙)
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
#include<stdio.h> #include<stdlib.h> int c1,c2,count=0,count_p=0,max=0,path=0,shortest=-1,current; struct road { int destination; int distance; struct road *next; }; struct node { int state; int prev; int num; struct road *head; struct road *tail; }*city; int city_dps() { int next; //int temp;显示节点用 count+=city[current].num; city[current].state=1; if(current==c2) { if(shortest!=-1) { if(shortest==path) { count_p++; if(count>=max) max=count; } else if(shortest>path) { shortest=path; count_p=1; max=count; } } else { shortest=path; count_p=1; max=count; } /* temp=c1;显示节点用 do { printf("%d->",temp); temp=city[temp].tail->destination; } while(temp!=c2); printf("%d#count:%d$%d\n",temp,count,path);*/ return 0; } while(city[current].tail!=NULL) { next=city[current].tail->destination; if(city[next].state!=-1) { city[current].tail=city[current].tail->next; } else { city[next].prev=current; path+=city[current].tail->distance; current=next; return 1; } } return 0; } int back_step() { int prev; if(current==c1) { count=0; return 0; } city[current].state=-1; city[current].tail=city[current].head; count-=city[current].num; prev=city[current].prev; current=prev; path-=city[current].tail->distance; city[current].tail=city[current].tail->next; count-=city[current].num; return 1; } int main() { int n,m,departure,destination,distance; int i; struct road *temp; scanf("%d%d%d%d",&n,&m,&c1,&c2); city=(struct node *)malloc(n*sizeof(struct node)); for(i=0;i<n;i++) { city[i].head=NULL; city[i].tail=NULL; city[i].state=-1; scanf("%d",&city[i].num); } city[c1].state=1; if(c2==c1) { printf("1 %d",city[c1].num); return 0; } for(i=0;i<m;i++) { scanf("%d%d%d",&departure,&destination,&distance); temp=(struct road *)malloc(sizeof(struct road)); temp->destination=destination; temp->distance=distance; temp->next=NULL; if(city[departure].head==NULL) { city[departure].head=temp; } else { city[departure].tail->next=temp; } city[departure].tail=temp; temp=(struct road *)malloc(sizeof(struct road)); temp->destination=departure; temp->distance=distance; temp->next=NULL; if(city[destination].head==NULL) { city[destination].head=temp; } else { city[destination].tail->next=temp; } city[destination].tail=temp; } for(i=0;i<n;i++) { city[i].tail=city[i].head; } current=c1; while(city[c1].tail!=NULL) { if(!city_dps()) { back_step(); } } printf("%d %d\n",count_p,max); } |