首页 > 代码库 > PAT1003. Emergency (25)
PAT1003. 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.
Sample Input
5 6 0 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1
Sample Output
2 4
提交代码
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 false11 }; //是否访问 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 }
PAT1003. Emergency (25)