1003. 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 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
题目大意:n个城市m条路,每个城市有救援小组,所有的边的边权已知。
给定起点和终点,求从起点到终点的最短路径条数以及最短路径上的救援小组数目之和。

如果有多条就输出点权(城市救援小组数目)最大的那个
 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<stdlib.h>
 4 
 5 #define MAX 502
 6 #define INFI 9999999
 7 
 8 int G[MAX][MAX],nv,ne,c1,c2; //图,结点数,边数,起点,终点
 9 int know[MAX];              //访问标记
10 int distance[MAX];          //距离
11 int num[MAX];               //最短路径条数
12 int weight[MAX];           //每个点的权值
13 int w[MAX];                //最短路径上的点权值总和
14 
15 void CreateGraph();
16 void Dijkstra( );
17 int main()
18 {
19     int i;
20     scanf("%d%d%d%d",&nv,&ne,&c1,&c2);
21     for( i=0; i<nv; i++)
22         scanf("%d",&weight[i]);
23     CreateGraph();
24     Dijkstra();
25     printf("%d %d",num[c2],w[c2]);
26     return 0;
27 }
28 
29 void CreateGraph()
30 {
31     int i,j;
32     int v1,v2;
33     int dn;
34     for( i=0; i<nv; i++)
35         for( j=0; j<nv; j++)
36            G[i][j]=INFI;
37     for( i=0; i<ne; i++)
38     {
39         scanf("%d%d%d",&v1,&v2,&dn);
40         G[v1][v2]=G[v2][v1]=dn;  //无向图
41     }
42 }
43 void Dijkstra( )
44 {
45     int i,j,k;
46     int min;
47     for( i=0; i<nv; i++)
48     {
49         know[i]=0;
50         distance[i]=G[c1][i];  //更新起点到所有顶点的距离
51     }
52     distance[c1]=0;           //起点到起点的距离为0
53     w[c1]=weight[c1];
54     num[c1]=1;
55     for( i=1; i<nv; i++)
56     {
57         k=-1;
58         min = INFI;         
59         for( j=0; j<nv; j++)
60         {
61             if( !know[j] && distance[j]<min)
62             {
63                 k=j;
64                 min = distance[j];
65             }
66         }
67         if( k==-1) break;
68         know[k]=1;  //寻找到最短的距离,标记该点
69         for( j=0; j<nv; j++)
70         {
71             if( !know[j] && min+G[k][j]<distance[j])  //更新距离
72             {
73                 distance[j] = min+G[k][j];
74                 num[j] = num[k];
75                 w[j] = w[k] + weight[j];
76             }
77             else if( !know[j] && min+G[k][j]==distance[j])
78             {
79                 num[j]=num[j]+num[k];
80                 if( w[k]+weight[j]>w[j])
81                     w[j]=w[k]+weight[j];
82             }
83         }
84     }
85 }

在这个国度中,必须不停地奔跑,才能使你保持在原地。如果想要寻求突破,就要以两倍现在速度奔跑!
原文地址:https://www.cnblogs.com/yuxiaoba/p/8537854.html