码迷,mamicode.com
首页 > 其他好文 > 详细

PAT1018. Public Bike Management

时间:2015-02-09 21:27:22      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:

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
思路:Dijsktra+DFS其中DFS方式值得借鉴。
技术分享
  1 #include<cstdio>
  2 #include<algorithm>
  3 #include<vector>
  4 //#include<cstring>
  5 using namespace std;
  6 const int MAXV=510;
  7 const int INF=0x3fffffff;
  8 bool vis[MAXV]={
  9     false
 10 };
 11 int G[MAXV][MAXV];
 12 int d[MAXV];
 13 int weight[MAXV];
 14 int Cmax,N,Sp,M;
 15 vector<int>pre[MAXV];
 16 vector<int>tempath,path;
 17 int minNeed=INF,minRemain=INF;
 18 void Dijsktra(int s)
 19 {
 20     /*for(int i=0;i<=N;i++){
 21         pre[i].push_back(i);
 22     }*/
 23     d[s]=0;
 24     for(int i=0;i<=N;i++)
 25     {
 26         int min=INF,u=-1;
 27         for(int j=0;j<=N;j++)
 28         {
 29             if(!vis[j]&&d[j]<min)
 30             {
 31                 u=j;
 32                 min=d[j];
 33             }
 34         }
 35         if(u==-1)
 36           return ;
 37         vis[u]=true;
 38         for(int v=0;v<=N;v++)
 39         {
 40             if(!vis[v]&&G[u][v]!=INF)
 41             {
 42                 if(d[u]+G[u][v]<d[v])
 43                 {
 44                     d[v]=d[u]+G[u][v];
 45                     pre[v].clear();
 46                     pre[v].push_back(u);
 47                 }
 48                 else if(d[u]+G[u][v]==d[v])
 49                 {
 50                     pre[v].push_back(u);
 51                 }
 52             }
 53         }
 54     }
 55 }
 56 void DFS(int v)
 57 {
 58 
 59     if(v==0)
 60     {
 61         tempath.push_back(v);
 62         int need=0,remain=0;
 63         for(int i=tempath.size()-1;i>=0;i--)
 64         {
 65             int Id=tempath[i];
 66             if(weight[Id]>0)
 67             {
 68                 remain+=weight[Id];
 69              }
 70              else
 71              {
 72                  if(remain>abs(weight[Id]))
 73                  {
 74                      remain-=abs(weight[Id]);
 75                  }
 76                  else
 77                  {
 78                      need+=abs(weight[Id])-remain;
 79                      remain=0;
 80                  }
 81              }
 82         }
 83         if(need<minNeed)
 84         {
 85             minNeed=need;
 86             minRemain=remain;
 87             path=tempath;
 88         }
 89         else if(need==minNeed&&remain<minRemain)
 90         {
 91             minRemain=remain;
 92             path=tempath;
 93         }
 94         tempath.pop_back();
 95         return;
 96     }
 97     tempath.push_back(v);
 98     for(int i=0;i<pre[v].size();i++)
 99     {
100         DFS(pre[v][i]);
101     }    
102     tempath.pop_back();
103 }
104 int main(int argc, char *argv[])
105 {
106     fill(G[0],G[0]+MAXV*MAXV,INF);
107     fill(d,d+MAXV,INF); 
108     scanf("%d%d%d%d",&Cmax,&N,&Sp,&M);
109     for(int i=1;i<=N;i++)
110     {
111         scanf("%d",&weight[i]);
112         weight[i]-=Cmax/2;
113     }
114     for(int i=0;i<M;i++)
115     {
116         int x,y;
117         scanf("%d%d",&x,&y);
118         scanf("%d",&G[x][y]);
119         G[y][x]=G[x][y];
120     }    
121     Dijsktra(0);
122     DFS(Sp);
123     printf("%d ",minNeed);
124     for(int i=path.size()-1;i>=0;i--)
125     {
126         if(i!=0)
127           printf("%d->",path[i]);
128         else
129           printf("%d ",path[i]);
130     }
131     printf("%d\n",minRemain);
132     return 0;
133 }
View Code

 


PAT1018. Public Bike Management

标签:

原文地址:http://www.cnblogs.com/GoFly/p/4282300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!