您的位置:首页 > 其它

1018. Public Bike Management (30)

2017-10-15 20:00 363 查看
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:

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.

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找出所有最短路,找到所有最短路中需要带来车最少的,若相同则比较带回车最少的。

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
struct node{
int b,next,v;
};
typedef struct node no;
no p[500000];
int st[500000];
int size,n,sp,m;
int num[600];
int dis[600];
const int big=99999999;
void spfa()
{
int vis[600]={0};
int i,head;
for (i=0;i<=n;i++)
dis[i]=big;
queue<int>qu;
qu.push(0);
dis[0]=0;
while (!qu.empty())
{
head=qu.front();
qu.pop();
vis[head]=0;
for (i=st[head];i!=-1;i=p[i].next)
{
int b=p[i].b;
if (dis[b]>dis[head]+p[i].v)
{
dis[b]=dis[head]+p[i].v;
if (!vis[b])
{
qu.push(b);
vis[b]=1;
}
}
}
}
}
int re[600],t=0;int v[600]={0};
int ans[600],anst,ans1=big,ans2;
void dfs(int now,int sum,int d,int count)
{
if (d>dis[sp]) return;
if (d==dis[sp]&&now==sp)
{
if (sum<ans1||sum==ans1&&count<ans2)
{
ans1=sum;
ans2=count;
anst=t;
for (int i=0;i<anst;i++)
ans[i]=re[i];
}
}
for (int i=st[now];i!=-1;i=p[i].next)
{
int b=p[i].b;
if (!v[b]){
v[b]=1;
re[t++]=b;
int k=0,kk;
if (num[b]+count>size/2)
kk=num[b]+count-size/2;
else {
k=size/2-num[b]-count;
kk=0;
}
dfs(b,sum+k,d+p[i].v,kk);
t--;
v[b]=0;
}
}
}
void prin()
{
printf("%d 0",ans1);
for (int i=0;i<anst;i++)
printf("->%d",ans[i]);
printf(" %d\n",ans2);
}
int main()
{
int i,j,x,y,z;
scanf("%d%d%d%d",&size,&n,&sp,&m);
for (i=1;i<=n;i++)
scanf("%d",&num[i]);
int cnt=0;
memset(st,-1,sizeof(st));
for (i=0;i<m;i++)
{
scanf("%d%d%d",&x,&y,&z);
p[cnt].b=y;
p[cnt].next=st[x];
st[x]=cnt;
p[cnt].v=z;
cnt++;
p[cnt].b=x;
p[cnt].v=z;
p[cnt].next=st[y];
st[y]=cnt;
cnt++;
}
spfa();
num[0]=0;v[0]=1;
dfs(0,0,0,0);
prin();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pat-1018