solution Of 1018. Public Bike Management (30)

48 篇文章 0 订阅

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.

这里写图片描述
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.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 numbers: Cmax (<= 100), always an even number, is the maximum capacity of each station; N (<= 500), the total number of stations; Sp, 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 Ci (i=1,…N) where each Ci is the current number of bikes at Si respectively. Then M lines follow, each contains 3 numbers: Si, Sj, and Tij which describe the time Tij taken to move betwen stations Si and Sj. 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->S1->…->Sp. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of Sp 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


结题思路 :
题意要求我们找到从0点到故障点Sp的最短距离。
要求1:在最短路的前提下,从0点携带的车辆数量最少;
要求2:在从0点携带车辆最少的前提下,要求需要带回的车辆最少;
要求3:输入为无向图,所以需要注意输入过程中的指定节点并没有先后关系;
要点4:当前节点需要的车辆只能来源于上一个节点,当前节点多余的车辆只会流向下一个节点。
要点5:这里不能够单纯得进行dij的计算,比如:

0 1 1 
0 2 1
1 3 1 
2 3 1 
3 4 1 
3 5 1 
4 6 1
5 6 1

这里从节点0到节点6实际上有4条路径,但如果单纯利用dij来处理我们队每个节点都需要记录可能的路劲,因为懒,我们直接用dfs解决。

程序步骤:
第一步、我们建立二维链表结构存储路劲连接信息;
第二步、dfs爆搜查找最短路,在查找过程中更新最短路的路劲信息;
第三步、对上一步得到的所有路径分别计算需要携带的车数量和需要带回去的车的数量。

具体程序(AC)如下:

#include <iostream>
#include <vector>
#include <stack>
#define inf 0x3fffffff
using namespace std;
int cMax,N,sp,M;
struct node{
    int end;
    int cost;
    node(int a,int b){end=a;cost=b;}
};
vector<vector<node> >map;//二维链表
vector<vector<int> >result;//保存最短路的路径
vector<int> road;//计算过程中得到的路径信息
vector<bool> visited;//标记节点访问信息
int minDist=inf;
void dfs(int start,int end,int dist)//深度优先的暴搜
{
    if(start==end)
    {
        if(minDist>dist)
        {
            minDist=dist;
            result.clear();
            result.push_back(road);
        }
        else if(minDist==dist)
            result.push_back(road);
        return;
    }
    int j;
    for(j=0;j<map[start].size();++j)
    {
        if(!visited[map[start][j].end])
        {
            road.push_back(map[start][j].end);
            visited[start]=1;
            dfs(map[start][j].end,end,dist+map[start][j].cost);
            visited[start]=0;
            road.pop_back();
        }
    }
    return ;
}
int main()
{
    cin>>cMax>>N>>sp>>M;
    map.resize(M);
    visited.resize(N+1,0);
    visited[0]=1;
    vector<int> cur(N+1);
    cur[0]=0;
    for(int i=1;i<=N;++i)
        cin>>cur[i];
    int start,end,cost;
    for(int i=0;i<M;++i)
    {
        cin>>start>>end>>cost;
        map[start].push_back(node(end,cost));
        map[end].push_back(node(start,cost));
    }
    //输入结束
    dfs(0,sp,0);
    int take=0,back=0;
    int minBack=inf,cIndex,minTake=inf;
    vector<int> copyOfCur;
    int i,j;
    for(i=0;i<result.size();++i)
    {
        //计算每条可行最短路劲需要的take和back
        copyOfCur=cur;
        take=0;
        back=0;
        for(j=0;j<result[i].size()-1;++j)
            if(copyOfCur[result[i][j]]>cMax/2)//计算前n-1个节点会流向下一个节点的车的数量
                copyOfCur[result[i][j+1]]+=copyOfCur[result[i][j]]-cMax/2;
            else if(copyOfCur[result[i][j]]<cMax/2)//当前节点不够的是需要0节点的车辆来补充的
                take+=cMax/2-copyOfCur[result[i][j]];
        if(copyOfCur[result[i][j]]>cMax/2)//最后一个节点多余的就是需要带回去的
            back=copyOfCur[result[i][j]]-cMax/2;
        else if(copyOfCur[result[i][j]]<cMax/2)//当前节点不够的是需要0节点的车辆来补充的

            take+=cMax/2-copyOfCur[result[i][j]];
        //计算每条可行最短路劲需要的take和back
        if(take<minTake)
        {
            minBack=back;
            cIndex=i;
            minTake=take;
        }
        else if(take==minTake&&back<minBack)
        {
            minBack=back;
            cIndex=i;
            minTake=take;
        }
    }
    cout<<minTake<<" "<<0;
    for(j=0;j<result[cIndex].size();++j)
            cout<<"->"<<result[cIndex][j];
    cout<<" "<<minBack<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值