1018. Public Bike Management (30)
2015-07-25 10:50
387 查看
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 inperfect 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.
![](http://www.patest.cn/upload/11_lrrk0m5o4kg.jpg)
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:
Sample Output:
提交代码
——————————
关于图的问题,一个DFS。
最近关于图的问题,自己又有点不怎么熟悉了,主要是将DFS应用在具体的问题中。
思路来自
主要的问题是在DFS中,在递归中,出口是在递归到出问题的节点。
在出口处需要处理当前len和最小len相等的情况,然后看最小的send,最后看最小的bike。
对于递归,curlen为相加。在递归返回的时候,如果不是最小的话,就pop出来,思路比较混乱。
我觉得可以用最小生成树的方法来做哦。
The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be inperfect 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.
![](http://www.patest.cn/upload/11_lrrk0m5o4kg.jpg)
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
提交代码
——————————
关于图的问题,一个DFS。
最近关于图的问题,自己又有点不怎么熟悉了,主要是将DFS应用在具体的问题中。
思路来自
主要的问题是在DFS中,在递归中,出口是在递归到出问题的节点。
在出口处需要处理当前len和最小len相等的情况,然后看最小的send,最后看最小的bike。
对于递归,curlen为相加。在递归返回的时候,如果不是最小的话,就pop出来,思路比较混乱。
我觉得可以用最小生成树的方法来做哦。
#include <iostream> #include <stdlib.h> #include <memory> #include <algorithm> #include <vector> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ #define MAX 510 #define INF 1<<30 struct graph{ int matrix[MAX][MAX]; int num; bool visited[MAX]; }* g; int cmax,N=0,sp=0,M=0; int s[MAX]; void ini(){ g=(graph *)malloc(sizeof(struct graph)); for(int i=0; i<=N; i++){ for(int j=0; j<=N; j++){ g->matrix[i][j]=0; } g->visited[i]=false; } g->num=N+1; for(int i=0; i<M; i++){ int si,sj,tij; cin>>si>>sj>>tij; g->matrix[si][sj]=g->matrix[sj][si]=tij; } } int curlen=0,minlen=INF,curbike=0,minbike=INF; int cursend=0,minsend=INF; vector<int> curroad,minroad; void dfs(int cur){ if(curlen>minlen) return; if(cur==sp){ bool choosed=false; if(curlen<minlen) choosed=true; else if(curlen==minlen){ if(cursend<minsend){ choosed=true; }else if(cursend == minsend){ if(curbike<minbike){ choosed=true; } } } if(choosed){ minlen=curlen; minsend=cursend; minbike=curbike; minroad=curroad; } return; } for(int i=0; i<=N; i++){ if(!g->visited[i] && g->matrix[cur][i]>0){ g->visited[i]=true; curlen+=g->matrix[cur][i]; int lastcursend=cursend; int lastcurbike=curbike; if(curbike+s[i]<cmax/2){ cursend+=cmax/2-(s[i]+curbike); curbike=0; }else{ curbike=(s[i]+curbike)-cmax/2; } curroad.push_back(i); dfs(i); curroad.pop_back(); cursend=lastcursend; curbike=lastcurbike; curlen-=g->matrix[cur][i]; g->visited[i]=false; } } } int main(int argc, char** argv) { cin>>cmax>>N>>sp>>M; for(int i=1; i<=N; i++) cin>>s[i]; ini(); g->visited[0]=true; dfs(0); printf("%d ",minsend); printf("%d",0); for(int i=0;i<minroad.size();i++){ printf("->%d",minroad[i]); } printf(" %d",minbike); return 0; }
相关文章推荐
- tc633div1 550分
- java中的流
- [编程之美]写一个函数,返回一个数组中所有元素被第一个元素除的结果
- 日志快速增长导致磁盘撑爆的快速解决方法
- 全备份失败后,如何手工清除exchange日志文件,附微软KB
- JAVA & PHP 结合 开发系统网站
- poj解题报告——1579
- GTK入门学习:布局容器之垂直布局
- 20个最常用的javascript方法函数
- mysql支持emoji表情的方法
- 如何选择适合自己企业的HR软件?
- Set Matrix Zeros
- CF 55D - Beautiful numbers(数位DP)
- Openssl oscp命令
- ZOJ 3785
- 裸机驱动模板
- [蠢哭了]Finder边栏中的“文稿”不见了
- 也谈VC中ModifyStyle&ModifyStyleEx无法改变控件的Style)
- 循环队列的基本操作
- 即时通讯工具中联系人相关专利