1. 程式人生 > >最短路_1018 Public Bike Management (30 分)

最短路_1018 Public Bike Management (30 分)

1018 Public Bike Management (30 分)

There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city.

The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect

condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well.

When a problem station is reported, PBMC will always choose the shortest path to reach that station. If there are more than one shortest path, the one that requires the least number of bikes sent from PBMC will be chosen.

The above figure illustrates an example. The stations are represented by vertices and the roads correspond to the edges. The number on an edge is the time taken to reach one end station from another. The number written inside a vertex S is the current number of bikes stored at S. Given that the maximum capacity of each station is 10. To solve the problem at S​3​​, we have 2 different shortest paths:

  1. PBMC -> S​1​​ -> S​3​​. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S​1​​ and then take 5 bikes to S​3​​, so that both stations will be in perfect conditions.

  2. PBMC -> S​2​​ -> S​3​​. This path requires the same time as path 1, but only 3 bikes sent from PBMC and hence is the one that will be chosen.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 numbers: C​max​​ (≤100), always an even number, is the maximum capacity of each station; N (≤500), the total number of stations; S​p​​, the index of the problem station (the stations are numbered from 1 to N, and PBMC is represented by the vertex 0); and M, the number of roads. The second line contains N non-negative numbers C​i​​ (i=1,⋯,N) where each C​i​​ is the current number of bikes at S​i​​ respectively. Then M lines follow, each contains 3 numbers: S​i​​, S​j​​, and T​ij​​ which describe the time T​ij​​ taken to move betwen stations S​i​​ and S​j​​. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print your results in one line. First output the number of bikes that PBMC must send. Then after one space, output the path in the format: 0−>S​1​​−>⋯−>S​p​​. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of S​p​​ is adjusted to perfect.

Note that if such a path is not unique, output the one that requires minimum number of bikes that we must take back to PBMC. The judge's data guarantee that such a path is unique.

Sample Input:

10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1

Sample Output:

3 0->2->3 0

解題歷程

因為普遍喜歡使用優先佇列+鏈式前向星的最短路求解問題,所以開始一直使用哪種方法,但是最後一組樣例總是記憶體超限,所以換做O(N^2)的Dijkstra演算法,這樣才過最後一組樣例

思路參考:https://blog.csdn.net/CV_Jason/article/details/81385228

首先我們要求求解出所有的最短路,和前面遇到的多條最短路的題目類似,只是這裡要對最路徑做的處理會麻煩一些,所以使用vector<int> per[maxn],將每個節點通過的路徑都儲存了起來,這樣後面再對最短路中的路徑進行深搜,然後判斷路徑上的需要帶的數量以及需要帶回的數量,找到最優解,給出答案即可

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include <queue>
#include <vector>

using namespace std;
const int maxn = 510;
const int INF = 0x3f3f3f3f;
int c,n,s,m,C[maxn],cost[maxn][maxn],dis[maxn];
int need,take,minneed,mintake;
vector<int> per[maxn],path,tpath;
bool used[maxn];

void Dijkstra()
{
    int min_node,min_cost;
    memset(used,false,sizeof(used));
    for(int i = 0;i <= n;i ++)
    {
        dis[i] = cost[0][i];
        if(i != 0 && dis[i] != INF) per[i].push_back(0);
    }
    dis[0] = 0;used[0] = true;
    for(int i = 0;i <= n;i ++)
    {
        min_cost = INF;
        for(int j = 0;j <= n;j ++)
        {
            if(used[j] == 0 && min_cost > dis[j])
            {
                min_cost = dis[j];
                min_node = j;
            }
        }
        used[min_node] = true;
        for(int j = 0;j <= n;j ++)
        {
            if(dis[j] > min_cost + cost[min_node][j])
            {
                per[j].clear();
                per[j].push_back(min_node);
                dis[j] = min_cost + cost[min_node][j];
            }
            else if(dis[j] == min_cost + cost[min_node][j])
            {
                per[j].push_back(min_node);
            }
        }
//         for(int i = 0;i <= n;i ++)
//        {
//            for(int j = 0;j < per[i].size();j ++)
//                cout << per[i][j]<<" ";
//            cout <<endl;
//        }
    }
}
void dfs(int p)
{
    tpath.push_back(p);
    if(p == 0)
    {
        need = 0,take = 0;
        for(int i = tpath.size() - 1;i >= 0;i --)
        {
            int id = tpath[i];
            if(C[id] >= 0)
                take += C[id];
            else
            {
                if(take >= -1 * C[id])
                    take += C[id];
                else
                {
                    need += -1 * C[id] - take;
                    take = 0;
                }
            }
        }
        if(need  <  minneed || (need  == minneed && take < mintake))
        {
            minneed = need;mintake = take;path = tpath;
        }
        tpath.pop_back();
        return ;
    }
    for(int i = 0;i < per[p].size();i ++)
    {
        int num = per[p][i];
        dfs(num);
    }

    tpath.pop_back();
    return ;
}
int main()
{
    minneed = INF,mintake = INF;
    scanf("%d%d%d%d",&c,&n,&s,&m);
    memset(cost,INF,sizeof(cost));

    for(int i = 1;i <= n;i ++)
        scanf("%d",C+i),C[i] -= c/2;
    int x,y,z;
    for(int i = 1;i <= m;i ++)
    {
        scanf("%d%d%d",&x,&y,&z);
        cost[x][y] = cost[y][x] = z;
    }
    Dijkstra();

    dfs(s);
    printf("%d 0",minneed);
    for(int i = path.size() -2;i >= 0;i --)
        printf("->%d",path[i]);
    printf(" %d\n",mintake);
    return 0;
}