1018. Public Bike Management (30)

来源:互联网 发布:庆元论坛淘宝乐园 编辑:程序博客网 时间:2024/05/17 19:59

1018. Public Bike Management (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 Spis 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 56 7 00 1 10 2 10 3 31 3 12 3 1
Sample Output:
3 0->2->3 0

提交代码

http://www.patest.cn/contests/pat-a-practise/1018

#include <stdio.h>#include <iostream>#include <algorithm>#include <vector>#include <set>#include <functional>#include <string>#include <queue> using namespace std; #define N 505 #define INF 1 << 30 int Cmax , n , sp , m  ;int Ci[N] ;int mp[N][N] ;bool vis[N] ;int min_dist = INF ;int cur_dist = 0 ;int min_send = INF ;int cur_send = 0 ;int min_bike = INF ;int cur_bike = 0 ;vector<int> cur_path ; vector<int> min_path ;void dfs(int cur){  if(cur_dist > min_dist)    return ;  if(cur == sp)  {    bool flag = false ; //flag 为true 表示结果需要更新    if(cur_dist < min_dist)    {      flag = true ;    }    else if(cur_dist == min_dist)    {      if(cur_send < min_send)      {        flag = true ;      }      else if(cur_send == min_send)      {        if(cur_bike < min_bike)        {          flag = true ;        }      }    }    if(flag)    {      min_dist = cur_dist ;      min_send = cur_send ;      min_bike = cur_bike ;      min_path = cur_path ; // vector 路径也直接替换    }    return ;  }  int i ;  for( i = 0 ;i <= n ; i ++)  {    if(mp[cur][i] > 0 && !vis[i] )    {      cur_dist += mp[cur][i] ;      vis[i] = true ;      // 记录当前的 主要是后面要回溯      int lastCurSend = cur_send ;      int lastCurBike = cur_bike ;      // 要送车辆      if(Ci[i] + cur_bike < Cmax/2)       {        cur_send += Cmax/2 - ( Ci[i] + cur_bike ) ;        cur_bike = 0 ;      }else{ // 要带走车辆        cur_bike = Ci[i] + cur_bike  - Cmax/2 ;        //cur_send = 0 ;      }      cur_path.push_back(i) ; // 添加到路径队列中      dfs(i) ;      // 回溯      cur_path.pop_back() ;      cur_send = lastCurSend ;      cur_bike = lastCurBike ;      cur_dist -= mp[cur][i] ; // 距离减掉      vis[i] = false ;    }  }}int main(){  //freopen("in.txt" , "r" , stdin) ;    scanf("%d%d%d%d" , &Cmax , &n , &sp , &m) ;  int i ;    for(i = 1 ; i <= n ; i ++)    {        scanf("%d" , &Ci[i]);     }  int u , v , dist ;     for(i = 0 ; i < m ; i ++)    {        int a,b,c;        scanf("%d%d%d" , &u , &v , &dist) ;        mp[u][v] = dist ;        mp[v][u] = dist ;    }  vis[0] = true ;  dfs( 0 ) ;    printf("%d " , min_send);    printf("%d" , 0);    for(i = 0 ; i < (int)min_path.size() ; i ++)    {        printf("->%d" , min_path[i]);    }    printf(" %d\n",min_bike);    return 0;}

此题的DFS ,回溯注意


cur_dist 即 0到0的距离 为0

cur_send,cur_bike都为0,表示 不需要携带任何车辆,也不需要带回任何车辆


这些变量 可以在dfs函数的参数中,为了避免参数太多,放到了外面


在判断结果的时候,按照 cur_dist最小, cur_send 最小,cur_bike最小的三个顺序 逐步的判断然后 结果改变,类似Dijkstra的各种变式题那样

(此题,用Dijkstra有几个case过不了,可能不是Dijkstra问题)


AC代码学习参考:http://my.oschina.net/superpdm/blog/161394

0 0
原创粉丝点击