zoukankan      html  css  js  c++  java

# 1018 Public Bike Management (30分)

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. The above figure 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

1. PBMC ->

2. PBMC ->

### Input Specification:

Each input file contains one test case. For each case, the first line contains 4 numbers:

### 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. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of

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这题需要注意两个点第一点：当存在多条最短路sent最少时，输出back最小的那条最短路。也就是说优先级有三个。第二点：记住回溯保存中间状态。
/*
本题思路：
1：求出最短路径
2：利用dfs，求出每条最短路的具体路径，以及在某条路径上需要派遣和遣返
的自行车的数量。
3：将所有路径按照，派遣数目增序进行排序，派遣数目相同的按照遣返数目
的增序进行排列，输出第一个答案。
*/
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

const int maxn = 500 + 5, inf = 0x3f3f3f3f;

int t, c;

struct Edge {
int to, cost, next;
} edges[maxn * maxn];

void addedge(int u, int v, int w) {
}

void init() {
cnt = 0;
}

bool vis[maxn];

int dist[maxn], cap[maxn];

void spfa(int s, int t) {
memset(dist, inf, sizeof dist);
memset(vis, false, sizeof vis);
queue <int> que;
que.push(s);
vis[s] = true;
dist[s] = 0;
while(!que.empty()) {
int u = que.front();
que.pop();
vis[u] = false;
for(int k = head[u]; ~k; k = edges[k].next) {
int v = edges[k].to;
if(dist[v] > dist[u] + edges[k].cost) {
dist[v] = dist[u] + edges[k].cost;
if(!vis[v]) {
vis[v] = true;
que.push(v);
}
}
}
}
}

struct Ans {
int sent, back;
vector <int> path;
} ans[maxn * maxn];
int num;

bool cmp(Ans a, Ans b) {
if(a.sent == b.sent) return a.back < b.back;
else return a.sent < b.sent;
}

int pre[maxn];

void dfs(int u) {
if(u == t) {
int now = u;
while(~ now) {
ans[num].path.push_back(now);
now = pre[now];
}
num ++;
return;
}
for(int k = head[u]; ~k; k = edges[k].next) {
int v = edges[k].to;
int temp1 = ans[num].sent;
int temp2 = ans[num].back;
if(dist[v] == dist[u] + edges[k].cost) {
pre[v] = u;
if(cap[v] > c / 2) {
ans[num].back += cap[v] - c / 2;
}
else {
if(ans[num].back  > c / 2 - cap[v]) ans[num].back  -= (c / 2 - cap[v]);
else {
ans[num].sent += c / 2 - cap[v] - ans[num].back ;
ans[num].back  = 0;
}
}
dfs(v);
ans[num].sent = temp1;
ans[num].back = temp2;
}
}
}

int main() {
init();
int n, m, u, v, w;
scanf("%d %d %d %d", &c, &n, &t, &m);
for(int i = 1; i <= n; i ++) scanf("%d", &cap[i]);
while(m --) {
scanf("%d %d %d", &u, &v, &w);
}
memset(pre, -1, sizeof pre);
spfa(0, t);
num = 0;
dfs(0);
sort(ans, ans + num, cmp);
printf("%d ", ans.sent);
for(int i = ans.path.size() - 1; i > 0; i --) {
printf("%d->", ans.path[i]);
}
printf("%d", t);
printf(" %d
", ans.back);
return 0;
}

• 相关阅读:
剑指offer---链表中倒数第k个结点
剑指offer---反转链表
剑指offer---从尾到头打印链表