您的位置:首页 > 其它

PAT (Advanced Level) Practise 1018 Public Bike Management (30)

2017-07-20 12:41 447 查看


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 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


题意:有一个公共自行车网,从起点出发到终点,要把除起点外的所有点的自行车数都置为half-full,找到最优路径,优先级依次为:路径长度最短, 从起点拿出去的车最少,从终点拿回来的车最少

解题思路:先做一次Dijkstra,求出起点到其他点的最短路,然后二分从起点拿出去的车,同时可以求出从终点拿回来的最少的车,最后在从终点反向dfs求出路径

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int C, n, ee, m;
int s[505], nt[50009], e[50009], l[50009],mi;
int dis[505], vis[505],a[505];

struct node
{
int id, dis;
bool operator <(const node &a) const
{
return dis > a.dis;
}
}pre,nt1;

void Dijkstra()
{
memset(vis, 0, sizeof vis);
memset(dis, INF, sizeof dis);
dis[0]=0;
pre.id = 0, pre.dis = 0;
priority_queue<node>q;
q.push(pre);
while (!q.empty())
{
pre = q.top();
q.pop();
vis[pre.id] = 1;
for (int i = s[pre.id]; ~i; i = nt[i])
{
if (vis[e[i]]) continue;
if (dis[e[i]] > dis[pre.id] + l[i])
{
dis[e[i]] = dis[pre.id] + l[i];
nt1.id = e[i], nt1.dis = dis[e[i]];
q.push(nt1);
}
}
}
}

void dfs(int x, int y)
{
if (y < 0) return;
if (x ==ee) { mi = min(mi, y); return; }
for (int i = s[x]; ~i; i=nt[i])
if (dis[e[i]] == dis[x] + l[i]) dfs(e[i], y - (C/ 2 - a[e[i]]));
}

int DFS(int x, int y, int z)
{
if (!x)
{
if (y == z)
{
printf("0");
return 1;
}
return 0;
}
for (int i = s[x]; ~i; i=nt[i])
{
if (dis[x] == dis[e[i]] + l[i])
{
if (DFS(e[i], y + C / 2 - a[x], z))
{
printf("->%d", x);
return true;
}
}
}
return false;
}

int main()
{
while (~scanf("%d%d%d%d", &C, &n, &ee, &m))
{
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
memset(s, -1, sizeof s);
int cnt = 1, u, v, w;
for (int i = 1; i <= m; i++)
{
scanf("%d%d%d", &u, &v, &w);
nt[cnt] = s[u], s[u] = cnt, e[cnt] = v, l[cnt++] = w;
nt[cnt] = s[v], s[v] = cnt, e[cnt] = u, l[cnt++] = w;
}
Dijkstra();
int l = 0, r = C*n / 2,ans;
while (l <= r)
{
int mid = (l + r) >> 1;
mi = INF;
dfs(0, mid);
if (mi < INF) {ans=mid, r = mid - 1; }
else l = mid + 1;
}
printf("%d ", ans);
mi = INF;
dfs(0, ans);
DFS(ee, mi, ans);
printf(" %d\n", mi);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: