题目链接:点击打开题目
时间限制 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:
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
这个机房找了半天网络快照工具都不能用,好气。
这个题折磨了两天了,终于拿到了满分30分。 题意:给出一个无向图,顶点的数字表示该点自行车数,边上数字表示通过需要的时间(可以理解为路程),给出一个PBMC(总部的意思),再给一个终点,要求: 1.从总部到终点要走最短的路。 2.路过的每一个点都要把该点的自行车数增减到完美(半满)。 3.要求从总部带去的自行车数和带回的自行车数越少越好。
解题报告: 首先是先做一遍迪杰斯塔拉,走到时候记录一下祖先。这样最短路做完就能得出所有的最短路径了。
然后是个核心的dfs的过程: dfs(p,num[p] > 0 ? max(Send-num[p],0) : Send - num[p] , num[p] > 0 ? (Send>num[p] ? Return : Return+(num[p]-Send)) : Return); 这里Send表示送来的车,Return表示送走的车,p为祖先节点。 仔细理解一下参数为什么那么写。
代码如下:
#include<queue>
#include<cmath>
#include<stack>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
#define CLR(a,b) memset(a,b,sizeof(a))
#define PI acos(-1.0)
int full,n,target,m;
int num[505]; //多余的车子数
struct Edge
{
int to;
int time;
};
vector<Edge> edge[505]; //存边
vector<int> father[505]; //记录祖先
struct Vertex
{
int pos;
int time;
bool friend operator < (Vertex a,Vertex b)
{
return a.time > b.time;
}
}pr,ne;
void dijkstra()
{
int time[505];
CLR(time,INF);
bool vis[505] = {false};
priority_queue<Vertex> Q;
pr.pos = 0;
pr.time = 0;
Q.push(pr);
while (!Q.empty())
{
pr = Q.top();
Q.pop();
if (pr.pos == target)
break;
vis[pr.pos] = true;
for (int i = 0 ; i < edge[pr.pos].size() ; i++)
{
ne.pos = edge[pr.pos][i].to;
if (vis[ne.pos])
continue;
ne.time = pr.time + edge[pr.pos][i].time;
if (ne.time < time[ne.pos]) //时间更短就更新,并加入优先队列
{
time[ne.pos] = ne.time;
father[ne.pos].clear();
father[ne.pos].push_back(pr.pos);
Q.push(ne);
}
else if (ne.time == time[ne.pos]) //时间一样就加入祖先
{
father[ne.pos].push_back(pr.pos);
}
}
}
}
int ansSend = INF;
int ansReturn = INF;
queue<int> BestRoad; //记录最优路径
bool check(const int Send,const int Return) //判断是否为更优解
{
if (Send < ansSend)
return true;
else if (Send == ansSend)
return Return < ansReturn;
return false;
}
bool dfs(const int pos,const int Send,const int Return) //搜索最优解
{
if (pos == 0)
{
if (check(Send,Return))
{
while (!BestRoad.empty())
BestRoad.pop();
ansSend = Send;
ansReturn = Return;
return true;
}
return false;
}
bool isNewAns = false;
for (int i = 0 ; i < father[pos].size() ; i++)
{
int p = father[pos][i];
//下面这句对于参数的计算是核心,太重要啊!!!!
bool flag = dfs(p,num[p] > 0 ? max(Send-num[p],0) : Send - num[p] , num[p] > 0 ? (Send>num[p] ? Return : Return+(num[p]-Send)) : Return);
if (flag)
{
isNewAns = true;
BestRoad.push(pos);
}
}
return isNewAns;
}
int main()
{
scanf ("%d%d%d%d",&full,&n,&target,&m);
full >>= 1;
for (int i = 1 ; i <= n ; i++)
{
scanf ("%d",&num[i]);
num[i] -= full;
}
for (int i = 1 ; i <= m ; i++)
{
Edge p;
int x,y,z;
scanf ("%d%d%d",&x,&y,&z);
p.time = z;
p.to = y;
edge[x].push_back(p);
p.to = x;
edge[y].push_back(p);
}
dijkstra(); //先搜出所有最短路
for (int i = 1 ; i <= n ; i++)
sort(father[i].begin(),father[i].end());
dfs(target,max(0,-num[target]),max(num[target],0));
cout << ansSend << " 0";
while (!BestRoad.empty())
{
cout << "->" << BestRoad.front();
BestRoad.pop();
}
cout << " " << ansReturn << endl;
return 0;
}
/*
//这组数据需要考虑到车子不能再运回去的情况
10 5 5 8
2 9 5 10 5
0 1 1
0 2 2
1 3 2
2 3 1
1 4 3
3 4 1
3 5 3
4 5 1
10 5 5 8
2 9 5 5 5
0 1 1
0 2 2
1 3 2
2 3 1
1 4 3
3 4 1
3 5 3
4 5 1
*/