5-35 Inter-City emergency rescue (25 points)
As the head of a city's emergency rescue team, you have a special national map. The map shows a number of scattered cities and some quick roads connecting the city. The number of rescue teams in each city and the length of each fast road connecting two cities are marked on the map. When other cities have an emergency call for you, your task is to lead your rescue team to the scene as quickly as possible, while gathering as many rescue teams along the way.
Input format:
Enter the first line to give 4 positive integers nn 、 mm 、 ss 、 Dd , where nn (2\le n\le 2 ≤ n ≤ 5 0 0 ) is the number of cities, by the way assuming the city number is 0 ~ (N-1) (N? 1 ); mm Is the number of fast roads; ss Is the city number of the departure point; Dd Is the city number of the destination.
The second line gives nn A positive integer, where section ii Number is the first ii The number of rescue teams in a city, separated by a space between the numbers. The subsequent mm line, each line gives a quick road information, respectively: City 1, City 2, the length of the fast road, the middle with a space separated, the numbers are integers and not more than 500. The input guarantees that rescue is feasible and the optimal solution is unique.
Output format:
The first line outputs the shortest path length and the number of rescue teams that can be called up. The second line outputs the ss To Dd The city number that is passed in the path. The numbers are separated by a space, and the output cannot have extra spaces at the end.
Input Sample:
4 5 0 320 30 40 100 1 11 3 20 3 30 2 22 3 2
Sample output:
2 600 1 3
#include "iostream" using namespace std; #define MAX 500#define Sky 99999int graph[max][max],top,start,end,edge;int dist[ Max],move[max],peo[max],sum[max],way[max];void Dis () {int i,j;for (i=0;i<top;i++) {dist[i]=graph[start][i]; Move[i]=false;if (I!=start && Graph[start][i]!=sky) {sum[i]=peo[start]+peo[i]; Way[i]=1;}} dist[start]=0; Move[start]=true; Sum[start]=peo[start]; Way[start]=1; for (i=1;i<=top-2;i++) {int minw,maxp;minw=maxp=sky;int k=start;for (j=0;j<top;j++) {if (! Move[j]) {if (Minw>dist[j]) {minw=dist[j];k=j;maxp=sum[j];} if (Minw==dist[j] && maxp<sum[j]) {maxp=sum[j];k=j;}}} Move[k]=true;for (j=0;j<top;j++) {if (! Move[j]) {if (Dist[j]==dist[k]+graph[k][j]) {way[j]+=way[k];if (Sum[j]<sum[k]+peo[j]) {sum[j]=sum[k]+peo[j];}} if (Dist[j]>dist[k]+graph[k][j]) {dist[j]=dist[k]+graph[k][j]; SUM[J]=SUM[K]+PEO[J]; WAY[J]=WAY[K];}}}} void Dfs (int end,int sump) {if (End==start) {return;} for (int j=0;j<top;j++) {if (Sump-peo[end]==sum[j] &&amP Dist[end]-graph[end][j]==dist[j]) {Dfs (j,sum[j]); cout<<j<< "";}}} int main () {//freopen ("1.txt", "R", stdin); cin>>top>>edge>>start>>end; int i,j; for (i=0;i<top;i++) for (j=0;j<top;j++) Graph[i][j]=sky; for (i=0;i<top;i++) cin>>peo[i]; for (i=0;i<edge;i++) {int x, y, Z, cin>>x>>y>>z; Graph[x][y]=graph[y][x]=z; } Dis (); cout<<way[end]<< "" <<Sum[End]<<endl; Dfs (End,sum[end]); cout<<end<<endl; return 0;}
5-35 Inter-City emergency rescue (25 points)