1018. Public Bike Management (30)-PAT
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
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
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 1Sample Output:
3 0->2->3 0
推薦指數:※※※※
這道題,首先想到的是最短路徑+dp,(註釋不能顯示中文,下次還是用英文好了)
#include<iostream>
#include<string.h>
#include<vector>
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;
#define N 600
int mapt[N][N], dist[N], visit[N], bikes[N];
vector<int> path[N];
#define maxnum 0xfffffff
void dijkstra(int n)//n¶¥µã¸öÊý
{
int i,j,u;
for(i=0;i<=n;i++){
visit[i]=0;
dist[i]=maxnum;
}
dist[0]=0;
for(i=0;i<=n;i++)//ɨÃèËùÓнڵã
{
int tmp=maxnum;
for(j=0;j<=n;j++){//ÕÒµ½Î´¼ÓÈ뼯ºÏµÄ×î½üµã
if(visit[j]==0&&dist[j]<tmp){
tmp=dist[j];
u=j;
}
}
visit[u]=1;//½«×î½üµã¼ÓÈ뼯ºÏ
for(j=0;j<=n;j++){
if(visit[j]==0){//¸üмÓÈëеãºóµÄ¾àÀë
if(dist[j]>dist[u]+mapt[u][j]){
dist[j]=dist[u]+mapt[u][j];//¸üоàÀë
path[j].clear();
path[j].push_back(u);//µ½´ï¸Ã½ÚµãµÄÇ°Ò»¸ö½Úµã
}
else if(dist[j]==dist[u]+mapt[u][j])
path[j].push_back(u);
}
}
}
}
vector<int> one_path,best_path;
int min_back_bike=maxnum,min_need_bike=maxnum;
int dp(int node,int capacity)
{
if(0==node){
int need_bike=0;
int back_bike=0;
for(int i=one_path.size()-2;i>=0;i--){//×îºóÒ»¸öÊÇ0½Úµã£¬²»Òª¼ÆËã
int t=one_path[i];
if(bikes[t]-capacity/2>0)//µ±Ç°³µÕ¾µÄ³µ×ÓÊýÁ¿¶àÓàÒªÇó
back_bike+=bikes[t]-capacity/2;
else if(bikes[t]-capacity/2<0){//µ±Ç°³µ×ÓÊýÁ¿²»×ã
back_bike=back_bike-(capacity/2-bikes[t]);
if(back_bike<0){//ÐèÒª³ö·¢Ê±Ð¯´øµÄÊýÁ¿
need_bike=need_bike+(-back_bike);
back_bike=0;
}
}
}
if(need_bike<min_need_bike){//·¾¶ÏàͬµÄÇé¿öÏÈ£¬Ñ¡Ôñ³öÃÅЯ´ø×îÉٵķ½°¸
min_need_bike=need_bike;
min_back_bike=back_bike;
best_path=one_path;
}
else if(need_bike==min_need_bike&&back_bike<min_back_bike){//³öÃÅЯ´øÏàͬµÄÇé¿öÏ£¬Ñ¡Ôñ´ý»á×îСµÄ·½°¸
min_back_bike=back_bike;
best_path=one_path;
}
return 0;
}
for(int i=0;i<path[node].size();i++){
one_path.push_back(path[node][i]);
dp(path[node][i],capacity);
one_path.pop_back();
}
return 0;
}
int main()
{
int capacity,n,sp;
int i,j,k;
long m;
cin>>capacity>>n>>sp>>m;//capacity, total stations,problem station,roads
for(i=0;i<=n;i++){
for(j=0;j<=n;j++){
mapt[i][j]=maxnum;
}
mapt[i][i]=0;
}
for(i=1;i<=n;i++)
cin>>bikes[i];
for(k=0;k<m;k++){
cin>>i>>j;
cin>>mapt[i][j];
mapt[j][i]=mapt[i][j];
}
dijkstra(n);
one_path.push_back(sp);
dp(sp,capacity);
cout<<min_need_bike<<" "<<0;
for(i=best_path.size()-2;i>=0;i--)
cout<<"->"<<best_path[i];
cout<<" "<<min_back_bike<<endl;
return 0;
}
其實dp就是選擇最短路徑中,need的車最少的情況,那麼其實可以從sp開始尋找到0的最短路徑,同時計算need的數量,感謝tossboy師兄的講解。
#include <iostream>
#include <vector>
using namespace std;
const int N=500+5;
int cap,n,sp,m;
int bikes[N];
vector<pair<int,int> > e[N];
int need[N], back[N];
int vis[N], dis[N], post[N];
bool first;
void dij(){
for(int i=0; i<n; i++){
dis[i] = -1;
need[i] = back[i] = -1;
}
dis[sp] = sp;
need[sp] = max(0, cap/2-bikes[sp]);
back[sp] = max(0, bikes[sp]-cap/2);
for(int ca=0; ca<n; ca++){
int mi=-1,u=-1;
for(int i=0; i<n; i++)
if(!vis[i] && dis[i]>=0 && (mi<0||dis[i]<mi))
mi = dis[i],u=i;
if(u==-1)
break;
vis[u]=1;
if(u==0)
return;
for(int i=0; i<e[u].size(); i++){
int v=e[u][i].first;
int w=e[u][i].second;
if(!vis[v]){
int nd=need[u], bk=back[u];
int x=bikes[v]-cap/2;
if(x>=0){ //more to back
if(x<=nd)
nd -= x;
else{
x -= nd;
nd =0;
bk += x;
}
}
else //need
nd += -x;
if(dis[v]<0 || dis[u]+w<dis[v]
|| dis[u]+w==dis[v] && nd<need[v]
|| dis[u]+w==dis[v] && nd==need[v] && bk<back[v]){
dis[v] = dis[u]+w;
need[v] = nd;
back[v] = bk;
post[v] = u;
}
}
}
}
}
int main(){
cin>>cap>>n>>sp>>m;
n++;
for(int i=1; i<n; i++)
cin>>bikes[i];
for(int i=0; i<m; i++){
int a,b,w; cin>>a>>b>>w;
if(w<=0)
return -1;
e[a].push_back(make_pair(b,w));
e[b].push_back(make_pair(a,w));
}
bikes[0]=cap/2;
dij();
cout<<need[0]<<" ";
int i=0;
while(i!=sp){
cout<<i<<"->";
i = post[i];
}
cout<<sp<<" "<<back[0]<<endl;
return 0;
}