默默吐槽下拼题网有些题目一些符号图片都显示不了..........
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 perfectcondition 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 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 S~3~, we have 2 different shortest paths:
1. PBMC -> S~1~ -> S~3~. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S~1~ and then take 5 bikes to S~3~, so that both stations will be in perfect conditions.
2. PBMC -> S~2~ -> S~3~. 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: C~max~ (<= 100), always an even number, is the maximum capacity of each station; N (<= 500), the total number of stations; S~p~, the index of the problem station (the stations are numbered from 1 to N, an d PBMC is represented by the vertex 0); and M, the number of roads. The second line contains N non-negative numbers C~i~ (i=1,...N) where each C~i~ is the current number of bikes at S~i~ respectively. Then M lines follow, each contains 3 numbers: S~i~, S~j~, and T~ij~ which describe the time T~ij~ taken to move betwen stations S~i~ and S~j~. 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->S~1~->...->S~p~. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of S~p~ 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
思路
每个单车站点有个最大容量,而站点拥有容量/2的车时认为是“完美”状态,要解决某一个站点的数量问题就从PBMC出发中途经过某些站点,可以带几辆车,可能PBMC也要带车,需要输出让途经的站点都是“完美”状态的最佳办法
C++:
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXV = 510;//最大顶点数
const int INF = 1000000000;//无穷大
//n为顶点数 m边数 Cmax最大容量 Sp为问题站点
//G为邻接矩阵 weight为点权 d[]记录最短距离
//minNeed记录最少携带的数目 minRemain 记录最少带回的数目
int n,m,Cmax,Sp,numPath=0,G[MAXV][MAXV],weight[MAXV];
int d[MAXV],minNeed = INF, minRemain = INF;
bool vis[MAXV] = {false};
vector<int> pre[MAXV]; //前驱
vector<int> tempPath, path; //临时路径及最优结构
void Dijkstra(int s){
fill(d, d+MAXV, INF);
d[s] = 0;
for(int i = 0; i <= n; i++){
int u = -1, MIN = INF;
for (int j = 0;j <= n;j++)
{
if (vis[j] == false && d[j] < MIN)
{
u = j;
MIN = d[j];
}
}
if (u == -1)return;//没有连通的点
vis[u] = true;//设置已经访问过
for (int v = 0;v <= n;v++)
{
if (vis[v] == false && G[u][v] != INF)
{
if (d[u] + G[u][v] < d[v])
{
d[v] = d[u] + G[u][v];//优化d[v]
pre[v].clear();
pre[v].push_back(u);
}else if(d[u] + G[u][v] == d[v])
{
pre[v].push_back(u);
}
}
}
}
}
void DFS(int v){
if(v == 0){//递归边界,叶子结点
tempPath.push_back(v);
int need = 0, remain = 0;
for (int i = tempPath.size()-1; i >= 0; i--)//此处倒着枚举
{
int id = tempPath[i];
if (weight[id] > 0 )
{
remain += weight[id];
}else{
if (remain > abs(weight[id]))
{
remain -= abs(weight[id]);
}else
{
need += abs(weight[id]) - remain;
remain = 0;
}
}
}
if (need < minNeed)//从PBMC所携带的最少
{
minNeed = need;
minRemain = remain;
path = tempPath;
}else if(need == minNeed && remain < minRemain)//携带数目相同 带回数目变少
{
minRemain = remain;
path = tempPath;
}
tempPath.pop_back();
return;
}
tempPath.push_back(v);
for (int i = 0;i < pre[v].size(); i++)
{
DFS(pre[v][i]);
}
tempPath.pop_back();
}
int main(){
scanf("%d %d %d %d",&Cmax, &n, &Sp, &m);
int u,v;
fill(G[0], G[0]+MAXV*MAXV, INF);
for (int i = 1; i <= n; i++)
{
scanf("%d",&weight[i]);
weight[i] -= Cmax/2;
}
for (int i = 0; i < m; i++)
{
scanf("%d %d",&u, &v);
scanf("%d",&G[u][v]);
G[v][u] = G[u][v];
}
Dijkstra(0);
DFS(Sp);
printf("%d ", minNeed);
for (int i = path.size()-1; i >= 0; i--)
{
printf("%d",path[i]);
if (i>0)printf("->");
}
printf(" %d",minRemain);
return 0;
}