• 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;
    }
  • 相关阅读:
    【开发者成长】喧哗的背后:Serverless 的挑战
    都在说实时数据架构,你了解多少?
    谊品生鲜:放弃传统数据库架构,全站上阿里云
    纯干货 | 一篇讲透如何理解数据库并发控制
    作为后端开发如何设计数据库系列文章(二)设计大数据量表结构
    如果千百年前有视觉AI算法,世界将会是什么样的光景呢?
    淘宝万亿级海量交易订单存储在哪?
    跬步千里 —— 阿里云Redis bitfield命令加速记
    容器环境自建数据库、中间件一键接入阿里云 Prometheus 监控
    常用Maven插件介绍(转载)
  • 原文地址:https://www.cnblogs.com/linruier/p/10085408.html
Copyright © 2020-2023  润新知