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 Output
2 4 ac代码:
//注意初始化时将所有e[i][j]所有初始化为max.不需要区分i=j;#include#include const int vmax=510;int dist[vmax];int visit[vmax];int n,c1,c2,per,count,maxper;//点数int path[vmax];int distance;//纪录到此结点能招到的人数;typedef struct{ int v[vmax]; int e[vmax][vmax]; int vnumb; int enumb;}graph;void dfs(graph *g,int i){// int ii,jj;// visit[i]=1; if(distance>dist[c2]) return;// per+=g->v[i];// printf("%d\n",i);// int pp; if(i==c2) { if(distance==dist[c2]) { count++; if(maxper vnumb;ii++) { if(i!=ii&&g->e[i][ii]!=-1&&visit[ii]==0) { per+=g->v[ii]; distance+=g->e[i][ii]; visit[ii]=1; dfs(g,ii); distance-=g->e[i][ii]; visit[ii]=0; per-=g->v[ii]; } }} void dijkstra(graph *g,int i){ int q,p; memset(visit,0,sizeof(visit)); int u=i; visit[u]=1; dist[u]=0;// path[u]=u; for(p=1;p e[u][q]!=-1&&dist[q]>dist[u]+(g->e[u][q])&&visit[q]==0)//找u,q之间存在边,且q!=u,未被访问,且距离更小 { dist[q]=dist[u]+g->e[u][q]; path[q]=u; } } int minval=9999999;//find the min dist[u];不可以赋初值为dist[0];注意,可能dist[0]就是最小的,就死循环了。; for(q=0;q vnumb=n; g->enumb=m; for(i=0;i e[i][j]=-1; else g->e[i][j]=0; } for(i=0;i v[i]); } for(i=0;i e[j][k]=t; g->e[k][j]=t; }/*for(i=0;i e[i][j]); } printf("\n");}*/ dijkstra(g,c1); // for(i=0;i v[c1]; visit[c1]=1; memset(visit,0,sizeof(visit)); dfs(g,c1); printf("%d %d\n",count,maxper); return 0;}