PAT-1003 Emergency(Dijkstra)

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.html

Input Specification:

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 C1- 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 , c1 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 C1.ios

Output Specification:

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

算法說明:

算法的大致意識是求出發城市到到目的城市的單元最短路徑,並記錄單元最短路徑的條數並輸出,在這些最短路徑中再求得能彙集醫療隊的最大數並輸出。
第一行輸入分別爲:城市的個數n,路徑的個數m,出發城市,目的城市
第二行輸入爲:各個城市醫療隊的數量
最後m行每行爲:c1 到c1以及權重
求單元最短路徑首先應該想到的是Dijkstra算法,單元最短路徑不止一條,而且要記錄出發城市到目的城市的醫療隊數量,能夠採用vector進行存儲,每一個節點的長度表明單元路徑條數,值表明醫療隊數量。

ui

城市醫療隊分佈圖


vector存儲結構

// 1003_Emergency.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string.h>
#include <vector>
#define MaxSize 500

using namespace std;

int Graph[MaxSize][MaxSize]; // 圖數組
int vis[MaxSize]={0},dis[MaxSize];
int curNum[MaxSize]={0};
int n,m,cur,des; // n 城市個數,m 爲路徑,cur 爲目前所在城市,des 爲目標城市
const int INF=0x7f;
vector<int> vec[MaxSize];
// 輸入數據
void Input(){
    int i,x,y,z;
    cin>>n>>m>>cur>>des;
    memset(Graph,-1,sizeof(Graph));
    for(i=0;i<n;i++)
        dis[i]=MaxSize;
    // 每一個城市的救援隊數量
    for(i=0;i<n;i++)
        cin>>curNum[i];
    // 輸入圖
    for(i=0;i<m;i++){
        cin>>x>>y>>z;
        Graph[x][y]=z;
        Graph[y][x]=z;
    }
}
void push(int target,int dataid) 
{
    for(int i=0;i<vec[dataid].size();i++)
        // 當前城市醫療隊數量+前一節點醫療隊數量
        vec[target].push_back(vec[dataid][i]+curNum[target]);
}
void Dijkstra(int cur,int des){
    int i,Min;
    int cen=cur;
    vis[cur]=1; // 已訪問
    vec[cur].push_back(curNum[cur]);
    dis[cur]=0;
    while(true){
        // 找到相鄰節點 更新距離
        for(i=0;i<n;i++){
            if(vis[i]==0&&Graph[cen][i]!=-1){
                if(Graph[cen][i]+dis[cen]<dis[i]){
                    dis[i]=Graph[cen][i]+dis[cen];
                    vec[i].clear(); // 清除當前城市數據節點
                    push(i,cen);
                }else if(Graph[cen][i]+dis[cen]==dis[i]){
                    push(i,cen);
                }
            }
        }
        // 找到下一節點
        Min=INF;
        for(i=0;i<n;i++){
            if(vis[i]==0&&dis[i]<Min){
                Min=dis[i];
                cen=i;
            }
        }
        vis[cen]=1;
        if(cen==des)
            break;
    }
}
int main(int argc, char* argv[])
{
    Input();
    Dijkstra(cur,des);
    vector<int> desVec=vec[des];
    cout<<desVec.size()<<' ';
    // 找單元最短路徑最大醫療隊數量
    int MaxValue=desVec[0];
    for(int i=1;i<desVec.size();i++){
        if(MaxValue<desVec[i]){
            MaxValue=desVec[i];
        }
    }
    cout<<MaxValue << endl;
    return 0;
}

2020考研打卡第一天,你可知道星辰之變,驕陽豈是終點?千萬不要小看一我的的決心。加油!!!

人生爲棋,我願爲卒,行動雖慢,可誰也沒見我後退一步。
相關文章
相關標籤/搜索