A_1003

作者: isjinhao | 来源:发表于2018-07-22 20:44 被阅读0次

    1003 Emergency (25)(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

    解题思路:dijkstra的变形。

    #include <bits/stdc++.h>
    #define INF 999999999
    using namespace std;
    class Dijkstra_Record{
        public:
            bool visited;
            int length;
            int num;
            int weight;
            Dijkstra_Record(){  visited = num = weight = 0; length = INF;} 
    };
    class MGraph{
        public:
            int **matrix;
            int vertexNum;
            int Dijkstra(int s, int v, MGraph *mg, int *weight, Dijkstra_Record *dr)
            {
                int n = mg->vertexNum, i, j;
                dr[s].num = 1;
                dr[s].length = 0;
                dr[s].weight = weight[s];
                for(i = 0; i < n; i++){
                    int min = INF, cur = -1;
                    for(j = 0; j < n; j++)
                        if(!dr[j].visited && dr[j].length < min)
                            min = dr[cur = j].length;
                    if(cur == -1)
                        return -1;
                    dr[cur].visited = true;
                    for(j = 0; j < n; j++){
                        int newLen = dr[cur].length + mg->matrix[cur][j];           
                        if(!dr[j].visited && mg->matrix[cur][j] != INF ) 
                            if(newLen < dr[j].length){
                                dr[j].length = newLen;
                                dr[j].num = dr[cur].num;
                                dr[j].weight = dr[cur].weight + weight[j];
                            }else if(newLen == dr[j].length){
                                dr[j].num = dr[j].num + dr[cur].num;
                                if(dr[j].weight < dr[cur].weight + weight[j])
                                    dr[j].weight = dr[cur].weight + weight[j];
                            }
                    }
                }
            }
    };
    int main()
    {
        int vertexNum, edgeNum, s, v, i, j;
        cin >> vertexNum >> edgeNum >> s >> v;
        int *weight = new int[vertexNum];
        for(i = 0; i < vertexNum; i++)
            cin >> weight[i];
        MGraph *mg = new MGraph();
        mg->vertexNum = vertexNum;
        mg->matrix = new int*[vertexNum];
        for(i = 0; i < vertexNum; i++){
            mg->matrix[i] = new int[vertexNum];
            fill(mg->matrix[i], mg->matrix[i] + vertexNum, INF);
        }
        int start, end, edgeWeight;
        for(i = 0; i < edgeNum; i++){
            cin >> start >> end >> edgeWeight;
            mg->matrix[start][end] = mg->matrix[end][start] = edgeWeight;
        }
        Dijkstra_Record *dr = new Dijkstra_Record[vertexNum];
        mg->Dijkstra(s, v, mg, weight, dr);
        cout << dr[v].num << " " << dr[v].weight;
        return 0;
    }  
    

    相关文章

      网友评论

          本文标题:A_1003

          本文链接:https://www.haomeiwen.com/subject/wisrmftx.html