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
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
提交代码
编译器
AWK (awk 4.0.1)
C (gcc 4.7.2)
C# (mcs 2.10.8.1)
C++ (g++ 4.7.2)
Go (gccgo 4.7.2)
Go (go 1.3)
Haskell (ghc 7.4.1)
Java (javac 1.6.0)
Java (gcj 4.7)
Javascript (node 0.10.33)
Lisp (clisp 2.49)
Lua (lua 5.2.1)
OCaml (ocamlc 3.12.1)
Pascal (fpc 2.6.0)
Perl (perl 5.14.2)
PHP (php 5.4.34)
Plaintext (cat 1.0)
Python (python3 3.2.3)
Python (python2 2.7.3)
Ruby (ruby 1.9.3)
Scheme (racket 5.2.1)
Shell (bash 4.2.37)
Vala (valac 0.16.1)
VisualBasic (vbnc 0.0.0.5943)
使用高级编辑器
代码
1
思路:Dijkstra算法 ,中间需要加以改变,增加的内容应为:当出现相同的最短路径时,需要记录个数,并且需要调整此路径中所有节点权值相加最大(即题中要求拥有最多的救生员)。
本人此题纠结了很长时间,出现的问题是没有仔细的看清楚题目,N和M搞混了,将节点数和路径数弄混了,消耗了好长时间做这道题,伤心。。。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int MAXV=510; 6 const int INF=0x3fffffff; 7 int G[MAXV][MAXV]; 8 int weight[MAXV]; //点权 9 bool vis[MAXV]={ 10 false 11 }; //是否访问 12 int d[MAXV]; //记录最短距离 13 int w[MAXV]; //到该点的最大点; 14 int num[MAXV]; //存储相同路径的条数 15 16 17 //number为城市的个数 18 void Dij(int C1,int C2,int number) 19 { 20 fill(d,d+MAXV,INF); 21 memset(num,0,sizeof(num)); 22 memset(w,0,sizeof(w)); 23 w[C1]=weight[C1]; 24 d[C1]=0; 25 num[C1]=1; 26 for(int i=0;i<number;i++) 27 { 28 int MIN=INF,u=-1;//min 与MIN 有差别????? 29 //寻找最短的路径 30 for(int j=0;j<number;j++) 31 { 32 if(!vis[j]&&d[j]<MIN) 33 { 34 MIN=d[j]; 35 u=j; 36 } 37 } 38 if(u==-1) 39 break; 40 vis[u]=true; 41 //从该路径进行开拓 42 for(int v=0;v<number;v++) 43 { 44 if(!vis[v]&&G[u][v]!=INF) 45 { 46 if(d[u]+G[u][v]<d[v]) 47 { 48 w[v]=w[u]+weight[v]; 49 d[v]=d[u]+G[u][v]; 50 num[v]=num[u]; 51 } 52 else if(d[u]+G[u][v]==d[v]) //点权 53 { 54 num[v]+=num[u]; 55 if(w[u]+weight[v]>w[v]) 56 w[v]=w[u]+weight[v]; 57 } 58 } 59 } 60 61 } 62 printf("%d %d\n",num[C2],w[C2]); 63 64 } 65 66 int main(int argc, char *argv[]) 67 { 68 fill(G[0],G[0]+MAXV*MAXV,INF); //fill函数初始化二维数组不同 69 70 int M,N,C1,C2; 71 scanf("%d%d%d%d",&N,&M,&C1,&C2); 72 for(int i=0;i<N;i++) 73 scanf("%d",&weight[i]); 74 for(int i=0;i<M;i++) //城市。。。。。。。。 75 { 76 int x,y,len; 77 scanf("%d%d%d",&x,&y,&len); 78 G[x][y]=len; //双向!!!!!! ?//???有问题? 79 G[y][x]=G[x][y]; 80 } 81 Dij(C1,C2,N); 82 83 return 0; 84 }