其他分享
首页 > 其他分享> > 1003 Emergency (25分)

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 Specification:

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C​1​​ and C​2​​ - 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 c​1​​, c​2​​ 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 C​1​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, 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

本题是一个单源最短路径问题。可以使用迪杰斯特拉算法求解:

求出C1到所有结点的最短路径,并统计最短路径的条数,分别保存在数组中。


AC代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string.h>
using namespace std;
typedef long long ll;
//Dijkstra  非指针版

#define INF 65535

#define maxn 1010
int Graph[maxn][maxn];//两点之间边长 
int data[maxn];//每个点的救援队数量 
int MaxRanks[maxn];//从c1到i的救援队最大数
int sumRoad[maxn];//从c1到i的最短路径数量
bool collected[maxn];//dijkstra收录点 
int dist[maxn];//从c1到i的长度
int num[maxn];//从c1到i最短路径条数 
int N,M,C1,C2;
int FindMin(){
    int minDist=INF;
    int minv=-1;
    for(int i=0;i<N;i++){
        if(collected[i]==false&&dist[i]<minDist){
            minDist=dist[i];
            minv=i;
        }
    }
    if(minDist<INF){
        return minv;
    } 
    else{
        return -1;
    }
}
void Dijkstra(int C1){
    int v,w;
    MaxRanks[C1]=data[C1];
    for(int i=0;i<N;i++){//对源点的邻接点初始化 
        dist[i]=Graph[C1][i];
        if(dist[i]<INF){
            num[i]=1;//源点的邻接点最短路径都为 1 
            MaxRanks[i]=MaxRanks[C1]+data[i];
        } 
    }
    num[C1]=1;
    dist[C1]=0;
    collected[C1]=1;
    while(1){
        v=FindMin();
        if(v==-1){
            break;
        }
        collected[v]=true;
        for(int i=0;i<N;i++){
            if(collected[i]==false&&Graph[v][i]<INF){
                if(dist[v]+Graph[v][i]<dist[i]){
                    dist[i]=dist[v]+Graph[v][i];
                    MaxRanks[i]=MaxRanks[v]+data[i];
                    num[i]=num[v];
                }else if(dist[v]+Graph[v][i]==dist[i]){
                    num[i]+=num[v];//如果路径相等,就相加 
//                    dist[i]=dist[v]+Graph[v][i];
                    if(MaxRanks[v]+data[i]>MaxRanks[i]){
                        MaxRanks[i]=MaxRanks[v]+data[i];
                    }
                }
            }
        }
    }
}
 
int main(){
    fill(Graph[0],Graph[0]+maxn*maxn,INF);
    memset(dist,INF,sizeof(dist)); 
    memset(MaxRanks,0,sizeof(MaxRanks));
    memset(sumRoad,0,sizeof(sumRoad));
    memset(collected,false,sizeof(collected));
    memset(num,0,sizeof(num));
    scanf("%d %d %d %d",&N,&M,&C1,&C2);
    for(int i=0;i<N;i++){
        scanf("%d",&data[i]);
    }
    int a,b,c;
    for(int i=0;i<M;i++){
        scanf("%d %d %d",&a,&b,&c);
        Graph[a][b]=c;
        Graph[b][a]=c;
    }
    
    Dijkstra(C1);
    printf("%d %d\n",num[C2],MaxRanks[C2]);
    return 0;
}

 

标签:25,cities,Emergency,int,memset,maxn,each,include,1003
来源: https://www.cnblogs.com/dre-52yao/p/14311117.html