zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    多线程——newFixedThreadPool线程池
    mysql SQL优化之嵌套查询-遁地龙卷风
    mysql练习题-查询同时参加计算机和英语考试的学生的信息-遁地龙卷风
    mysql存储过程编写-入门案例-遁地龙卷风
    编程轶事-java中的null-遁地龙卷风
    正逆向思维-编程轶事-遁地龙卷风
    MyBatis处理一行数据-MyBatis使用sum语句报错-MyBatis字段映射-遁地龙卷风
    MyBatis框架在控制台打印Sql语句-遁地龙卷风
    3d转换-正方体-Html5Css3-遁地龙卷风
    突破瓶颈-遁地龙卷风
  • 原文地址:https://www.cnblogs.com/linruier/p/10085408.html
Copyright © 2011-2022 走看看