solution Of 1018. Public Bike Management (30)
2016-07-06 16:50
459 查看
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:
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.
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到节点6实际上有4条路径,但如果单纯利用dij来处理我们队每个节点都需要记录可能的路劲,因为懒,我们直接用dfs解决。
程序步骤:
第一步、我们建立二维链表结构存储路劲连接信息;
第二步、dfs爆搜查找最短路,在查找过程中更新最短路的路劲信息;
第三步、对上一步得到的所有路径分别计算需要携带的车数量和需要带回去的车的数量。
具体程序(AC)如下:
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:
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.
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; }
相关文章推荐
- [书目20160706]成功销售实用经典10步骤(美国培训与发展协会实用经典10步系列)
- python vs ruby
- android_结构
- 阿里面经之解答 by cmershen(4)——线程池
- nginx如何防止ddos***
- 《UNIX环境高级编程》---1.UNIX基础知识
- 直播平台的高并发架构设计3.3-播放器端
- 简单掌握桶排序算法及C++版的代码实现
- 数字转换成指定格式的字符串
- (一)MVC5干货篇,目录和路由
- python setup.py用法
- 分分钟学会【A星寻路】~~~~~~~【无上天君与梦瑶的传说】
- Android 关机(reboot)流程 -- "sys.powerctl"
- 在cisco路由器上使用TFTP备份/恢复交换路由的IOS文件
- service 后台下载
- section 1.4 milk3
- 分析数字自动翻滚的动画---加深对属性动画的理解
- 华为机试题--13.求int型数据在内存中存储时1的个数
- selenium封装常用方法
- 重现PHP Core的调用栈