您的位置:首页 > 其它

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.



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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: