Public Bike Management (30)
时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)
题目描述
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 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:
输入描述:
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.
输出描述:
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.
输入例子:
10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1
输出例子:
3 0->2->3 0
思路:
1.先用dijkstra求出0号节点到所有点的最短路径长度dist[],路径path[],送出自行车send[],带回自行车rec[]。
2.遍历从0到sp经过的倒数第二个节点,从0到sp的最短路径=min(0->i的最短路径+i->sp最短路径)即dist[i]+station[sp],以求出从0->sp多条最短路径。从可能存在的多个最短路径中选取send最小,或send相等rec最小(这里rec为0或负值,所以应该是绝对值最小)
说明:g表示图,dist[i]表示从0到i号节点的最短路径长度,station[i]表示i号节点的相对自行车数量,path[i]表示最短路径中i号节点的前一节点,send[i]表示从0到i需要送出的bike,rec[i]表示从0到i需要接受的bike
#include
#include
#include
using namespace std;
const int N = 505, INF = 1000000000;
int n, m, c, sp;
int g[N][N], dist[N], station[N],path[N], send[N],rec[N];
bool st[N]; // 存储每个点的最短距离是否已确定
void dijkstra()
{
for (int i = 0; i<=n; i++) dist[i] = INF;
dist[0] = 0;
for (int i = 0; i<=n; i++)
{
int id, mind = INF;
for (int j = 0; j <= n; j++)
if (!st[j] && dist[j] < mind)
{
mind = dist[j];
id = j;
}
st[id] = 1;
for (int j = 0; j <= n; j++)
{
if(dist[id]+g[id][j]<dist[j])
{
dist[j]=dist[id]+g[id][j]; //更新最短路径长度
path[j]=id; //更新路径
if(rec[id]+station[j]>0) //更新send和rec
{
rec[j]=0;
send[j]=send[id]+rec[id]+station[j];
}
else
{
rec[j]=rec[id]+station[j];
send[j]=send[id];
}
}
}
}
}
void outpath()
{
vector<int>v;
for(int i=sp;i;i=path[i]) v.push_back(i);
for(int i=v.size()-1;i>=0;i--) cout<<"->"<<v[i];
}
int main()
{
cin >> c >> n>>sp>>m;
c=c/2;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
g[i][j] = INF;
for(int i=1,t;i<=n;i++)
{
cin>>t;
station[i]=c-t; //station[i]为正表示需要bike,为负表示多出bike
}
for (int i = 0; i < m; i++)
{
int a, b, c;
cin >> a >> b >> c;
g[a][b] = g[b][a] = min(g[a][b], c);
}
dijkstra();
/*for(int i=0;i<=n;i++) cout<
int mindis=dist[sp],minsend=send[sp],minrec=rec[sp];
for(int i=1;i<=n;i++) //求出多条路径,并选出send最小,rec最小
{
if(dist[i]+g[i][sp]==mindis)
{
int s,r;
if(rec[i]+station[sp]>0)
{
s=send[i]+rec[i]+station[sp];
r=0;
}
else
{
r=rec[i]+station[sp];
s=send[i];
}
if(s<minsend || (s==minsend && r>minrec))
{
path[sp]=i;
minsend=s;
minrec=r;
}
}
}
cout<<minsend<<" "<<0;
outpath();
cout<<" "<<-minrec<<endl;
return 0;
}