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 N1), 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 Specification:

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

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

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

指针版:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string.h>
using namespace std;
typedef long long ll;
const int maxn=1010;
#define INF 65535

int num[maxn];//最短路径的条数 
bool collected[maxn];//收录最短路径上的点 
int dist[maxn]; 
int sum[maxn];
int N,M,C1,C2;
//Dijkstra 指针版 
typedef struct ENode {
    int v1,v2;//有向边 
    int w;//权重 
}Enode,*ptrEnode;
typedef struct GNode{
    int nv;//顶点数 
    int ne;//边数 
    int G[maxn][maxn]; //边的值 
    int data[maxn];
}Gnode,*ptrGnode;

ptrGnode CreateGraph(int Nv){
    int v,w;
    ptrGnode Graph;
    Graph=new Gnode;
    Graph->nv=Nv;
    Graph->ne=0;
    for(int i=0;i<Graph->nv;i++){
        for(int j=0;j<Graph->nv;j++){
            Graph->G[i][j]=INF;
        }
    }
    for(int i=0;i<Graph->nv;i++){
        scanf("%d",&Graph->data[i]);
    }
    return Graph;
}
void InsertEdge(ptrGnode Graph,ptrEnode E){
    Graph->G[E->v1][E->v2]=E->w;
    Graph->G[E->v2][E->v1]=E->w;
}
void BuildGraph(ptrGnode &Graph,ptrEnode E){
    
    scanf("%d %d %d %d",&N,&M,&C1,&C2);
    int v;
    int Nv,i;
    Graph=CreateGraph(N);
    Graph->ne=M;
    if(Graph->ne!=0){
        E=new Enode;
        for(int i=0;i<Graph->ne;i++){
            scanf("%d %d %d",&E->v1,&E->v2,&E->w);
            InsertEdge(Graph,E);
        }
    }
}

int FindMinDist(ptrGnode Graph){
    int minv,v;
    int  minDist=INF;
    for(int i=0;i<Graph->nv;i++){
        if(collected[i]==0&&dist[i]<minDist){
            minDist=dist[i];
            minv=i;
        }
    } 
    if(minDist<INF){
        return minv;
    }
    else {
        return -1;
    }
} 
int Dijkstra(ptrGnode Graph){
    int v,w;
    sum[C1]=Graph->data[C1];
    //初始化
    for(int i=0;i<Graph->nv;i++){//对源点的邻接点初始化 
        dist[i]=Graph->G[C1][i];//0 2
        if(dist[i]<INF){
            num[i]=1; 
            sum[i]=sum[C1]+Graph->data[i];//i=2
        }
    } 
    num[C1]=1;
    dist[C1]=0;
    collected[C1]=true;
    while(1){
        v=FindMinDist(Graph);
        if(v==-1){
            break;
        }
        collected[v]=true;
        int maxp=0;
        for(int i=0;i<Graph->nv;i++){
            if(collected[i]==false){
                if(Graph->G[v][i]<0){
                    return 0;
                }
                if(dist[v]+Graph->G[v][i]<dist[i]){
                    dist[i]=dist[v]+Graph->G[v][i];
                    sum[i]=sum[v]+Graph->data[i];
                    num[i]=num[v];
                }else if(dist[v]+Graph->G[v][i]==dist[i]){
                    num[i]+=num[v];
                    if(sum[v]+Graph->data[i]>sum[i]){
                        sum[i]=sum[v]+Graph->data[i];
                    }
                }
            }
        }
    }
    return 1;
}


int main(){
    
    ptrGnode Graph;
    ptrEnode E;
    fill(num,num+maxn,0);
    fill(sum,sum+maxn,-1);
    fill(collected,collected+maxn,false);
    //创建图
    BuildGraph(Graph,E);
    //分析图
    if(Dijkstra(Graph)){
        printf("%d %d\n",num[C2],sum[C2]);
    }
    return 0;
}

非指针版:
#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);
    fill(dist,dist+maxn,INF); 
    memset(MaxRanks,0,sizeof(MaxRanks));
    memset(sumRoad,0,sizeof(sumRoad));
    fill(collected,collected+maxn,false);
    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;
}

 

 

 

posted @ 2021-01-21 23:25  XA科研  阅读(139)  评论(0编辑  收藏  举报