• PAT 1003. Emergency


    PAT 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
    

    分析

    这道题是Dijskta算法的改造对单源最短路径的问题

    代码如下

    #include<iostream>
    using namespace std;
    # define maxv 500
    # define maxdist 1000000 // 当两点之间没有边时,连接矩阵就赋值maxdist 
    int count=0,dist[maxv]; // dist[i]记录起始点到 i的最短路径 
    int collected[maxv]={0};  
    int num[maxv]={0}; // num[i]记录从起始点到 i最短路径有几条 
    int team[maxv]={0}; //team[i]记录从起始点到i能召唤的最多队伍数量 
    typedef struct Edge* edge; 
    struct Edge{
    	int v1,v2;
    	int weight;
    };
    typedef struct Graph* graph;
    struct Graph{
    	int Nv=0;
    	int Ne=0;
    	int data[maxv];
    	int G[maxv][maxv];
    };
    graph CreateGraph(int n){
    	graph gra=new Graph();
    	gra->Nv=n;
    	gra->Ne=0;
    	for(int i=0;i<n;i++)
    	for(int j=0;j<n;j++)
    	gra->G[i][j]=maxdist;
    	for(int i=0;i<n;i++){
    	gra->data[i]=0;	
    	dist[i]=maxdist;
    	}
    	return gra;
    }
    void insertedge(graph gra,edge e){
    	gra->G[e->v1][e->v2]=e->weight;
    	gra->G[e->v2][e->v1]=e->weight; 
    }
    int findmin(int dist[],int N){
    	int min=maxdist,t=-1;
    	for(int i=0;i<N;i++)
    	if(dist[i]<min&&collected[i]!=1){
    	min=dist[i]; t=i;	
    	}
    	return t;
    }
    void solve(graph gra,int c1,int c2){
        dist[c1]=0; int v1=c1,v2;
        team[c1]=gra->data[c1]; num[c1]=1;
    	while(1){
    	    int v1=findmin(dist,gra->Nv);
    	    collected[v1]=1;
    		if(v1==-1) break; 
    		for(v2=0;v2<gra->Nv;v2++)
    		if(collected[v2]!=1&&gra->G[v1][v2]!=maxdist){
    		if(gra->G[v1][v2]+dist[v1]<dist[v2]){
    		dist[v2]=gra->G[v1][v2]+dist[v1];
    		/*最短路径跟新时,最短路径数量就是前一个点的最短路径数量*/ 
    		num[v2]=num[v1]; 
    		/*最短路径跟新时,队伍数量就是前面的队伍数量加上该点地区的队伍数量*/ 
    		team[v2]=team[v1]+gra->data[v2];	
    		}
    		else if(gra->G[v1][v2]+dist[v1]==dist[v2]){
    			/*当到达某点的最短路径有多条时,加上新的最短路径条数*/ 
    			num[v2]+=num[v1];
    			/*当路径都是最短路径时取队伍最多的*/
    			team[v2]=team[v1]+gra->data[v2]>team[v2]?team[v1]+gra->data[v2]:team[v2];
    			}	
    		}
    		
    	}
    	cout<<num[c2]<<" "<<team[c2]<<endl;
    }
    int main(){
    	int n,m,c1,c2;
    	cin>>n>>m>>c1>>c2;
    	graph gra=CreateGraph(n);
    	for(int i=0;i<n;i++)
    	cin>>gra->data[i];
    	for(int i=0;i<m;i++){
    		edge e=new Edge();
    		cin>>e->v1>>e->v2>>e->weight;
    		insertedge(gra,e);
    	}
    	solve(gra,c1,c2);
    	return 0;
    } 
    
  • 相关阅读:
    HDU 5313 bitset优化背包
    bzoj 2595 斯坦纳树
    COJ 1287 求匹配串在模式串中出现的次数
    HDU 5381 The sum of gcd
    POJ 1739
    HDU 3377 插头dp
    HDU 1693 二进制表示的简单插头dp
    HDU 5353
    URAL 1519 基础插头DP
    UVA 10294 等价类计数
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/8185116.html
Copyright © 2020-2023  润新知