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

Figure 1
Figure 1 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 S3 , we have 2 different shortest paths:
1. PBMC -> S1 -> S3 . In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S1 and then take 5 bikes to S3 , so that both stations will be in perfect conditions.
2. PBMC -> S2 -> S3 . 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.

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
int c,n,m,des;
struct edge{int to,cost;};
typedef pair<int,int>pir;
vector<edge> e[505];
int d[505];
int num[505];
vector<int> pre[505];
void dijkstra()
{
    priority_queue<pir,vector<pir>,greater<pir> > q;
    while(!q.empty())q.pop();
    memset(d,inf,sizeof(d));
    d[0]=0;
    pir tmp(0,0);
    q.push(tmp);
    while(!q.empty())
    {
        tmp=q.top();q.pop();
        if(d[tmp.second]<tmp.first)continue;
        int id=tmp.second;
        for(int i=0;i<e[id].size();i++)
        {
            int tt=e[id][i].to;
            if(d[tt]>d[id]+e[id][i].cost)
            {
                d[tt]=d[id]+e[id][i].cost;
                q.push(pir(d[tt],tt));
                pre[tt].clear();
                pre[tt].push_back(id);
            }
            else if(d[tt]==d[id]+e[id][i].cost)
            {
                pre[tt].push_back(id);
            }
        }
    }
}
vector<int>ans;int ans1=0x3f3f3f3f,ans2=0x3f3f3f3f;
void dfs(vector<int>vec,int now)
{
    if(now==0)
    {
        int record=0;int a1=0;int sum=0;
        for(int j=vec.size()-1;j>=0;j--)
        {
            if(vec[j]==0)continue;
            int t=vec[j];
            sum+=num[t];
            if(num[t]==c/2)continue;
            else if(num[t]>c/2)record+=num[t]-c/2;
            else record-=c/2-num[t];
            if(record<0)a1=max(a1,-record);
        }
        int a2=a1+sum-(c/2)*(vec.size()-1);
        if(a1<ans1)
        {
            ans=vec;ans1=a1;ans2=a2;
            
        }
        else if(a1==ans1)
        {
            if(a2<ans2)
            {
                ans=vec;ans1=a1;ans2=a2;
            }
        }
 
        return;
    }
    vector<int>tmp;
    for(int i=0;i<pre[now].size();i++)
    {
        tmp=vec;tmp.push_back(pre[now][i]);
        dfs(tmp,pre[now][i]);
    }
}
int main()
{
    cin>>c>>n>>des>>m;
    for(int i=1;i<=n;i++)cin>>num[i];
    int a,b,cost;edge tmp;
    for(int i=0;i<m;i++)
    {
        cin>>a>>b>>cost;tmp.to=b;tmp.cost=cost;
        e[a].push_back(tmp);
        tmp.to=a;
        e[b].push_back(tmp);
    }
    dijkstra();
    vector<int>path;
    path.clear();
    path.push_back(des);
    dfs(path,des);
    cout<<ans1<<" ";
    for(int i=ans.size()-1;i>=0;i--)
    {
        cout<<ans[i];
        if(i!=0)printf("->");
    }
    cout<<" "<<ans2<<endl;
    return 0;
}

 

posted @ 2018-12-03 14:33  erge1998  阅读(164)  评论(0编辑  收藏  举报