1. 程式人生 > >1018. Public Bike Management (30)

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.


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

解題分析:

1.不能直接使用dijkstra,以下為直接使用dijkstra無法通過的資料,也就是測試點7的無法通過的原因:

10 4 4 5

6 7 5 0

0 1 1

0 2 1

1 3 1

2 3 1

3 4 1

2.可直接使用dfs或者dijkstra + dfs,以下為dfs AC的程式碼:
#include<iostream>
#include<vector>
using namespace std;
#define inf 999999
int cmax, n, sp, m, send,back;
int bike[505] = { 0 };
int route[505][505] = {{ 0 }};
int visit[505] = { 0 };
int minsend = inf, minback = inf, mindis = inf;
vector<int> path, temppath;
void dfs(int v,int tempsend,int tempback,int tempdis)
{
	temppath.push_back(v);
	visit[v] = 1;
	if(v == sp)
	{
		if(mindis > tempdis)
		{
			minsend = tempsend;
			minback = tempback;
			mindis = tempdis;
			path = temppath;
		}
		else if(mindis == tempdis)
		{
			if(minsend > tempsend)
			{
				minsend = tempsend;
				minback = tempback;
				path = temppath;
			}
			else if(minsend == tempsend && minback > tempback)
			{
				minback = tempback;
				path = temppath;
			}
		}
		return;
	}
	for(int i = 1; i <= n; i++)
	{
		if(visit[i] == 0&&route[v][i] != inf)
		{
			int temp = bike[i] - cmax;
			int tempb = tempback, temps = tempsend;
			if(temp >= 0)
				tempb += temp;
			else
			{
				if(tempback + temp < 0)
				{
					temps -= (tempback + temp);
					tempb = 0;
				}
				else
					tempb += temp;
			}
			dfs(i,temps,tempb,tempdis + route[v][i]);
			temppath.pop_back();
			visit[i] = 0;
		}
	}
}
int main()
{
	scanf("%d%d%d%d",&cmax,&n,&sp,&m);
	cmax /= 2;
	fill(route[0],route[0] + 505 * 505, inf);
	for(int i = 0; i < n; i++)
		scanf("%d",&bike[i + 1]); 
	for(int i = 0; i < m; i++)
	{
		int ti, tj, v;
		scanf("%d%d%d",&ti,&tj,&v);
		route[ti][tj] = route[tj][ti] = v;
	}
	dfs(0,0,0,0);	
	printf("%d ",minsend);
	for(int i = 0; i < path.size(); i++)
	{
		if(i != 0)
			printf("->");
		printf("%d",path[i]);
	}
	printf(" %d",minback);
	return 0;
}


相關推薦

1018 Public Bike Management (30)

const ios perf orm repr sts div break tput There is a public bike service in Hangzhou City which provides great convenience to the touris

PAT甲級1018 Public Bike Management (30)

1018 Public Bike Management (30 分) There is a public bike service in Hangzhou City which provides great convenience to the tourists from

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

PAT甲級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 r

1018 . Public Bike Management (30)自己寫的,只有14分

#include<iostream> #include<vector> using namespace std; class CA { public:  enum{C=100,N=501,INF=0x6FFFFFFF};  void run();  

PAT Advanced Level 1018. Public Bike Management (30)(Java and C++)

PAT Advanced Level 1018. Public Bike Management (30) 最短路徑問題。題意有三:1.時間最短 2.送出車輛最少 3.回收車輛最少     ps:(注意我的lessThan函式) 我的思路:是 SPFA(求出最短路徑)

1018. Public Bike Management (30)-PAT

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 stat

1018 Public Bike Management (30)(30 分)(C++)

1018 Public Bike Management (30)(30 分) There is a public bike service in Hangzhou City which provides great convenience to the tourists f

1018 Public Bike Management (30) Dijkstra算法 + DFS

== net pub blog sta 認識 pop false inf 題目及題解 https://blog.csdn.net/CV_Jason/article/details/81385228 迪傑斯特拉重新認識 兩個核心的存儲結構: int dis[n]

1018 Public Bike Management30 分)(Dij+DFS)

1018 Public Bike Management (30 分) There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over th

1018 Public Bike Management30 分)

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

1018 Public Bike Management30 分)(圖的遍歷and最短路徑)

    這題不能直接在Dijkstra中寫這個第一 標尺和第二標尺的要求 因為這是需要完整路徑以後才能計算的  所以寫完後可以在遍歷   #include<bits/stdc++.h> using namespace std; int c

PAT 1018 Public Bike Management30 分)

1018 Public Bike Management (30 分) There is a public bike service in Hangzhou City which provides great convenience to the tourists from all ove

【未完成】【笨方法學PAT】1018 Public Bike Management30 分)

一、題目 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

PATA 1018 Public Bike Management30 分)解題報告

1018 Public Bike Management(30 分) There is a public bike service in Hangzhou City which provides great convenience to the tourists from a

PAT1018. Public Bike Management (30)

pac size bool cst lan visit 最短路 路徑 vector 題目傳送門 分析   Dijstra + DFS搜索,難點在於存在多條最短路徑,因此需要用vector<int> pre[N] 記錄路徑。 代碼   自己寫的有些問題,有兩個數據

1018 Public Bike Management

ret content test describe format ostream require lse oos There is a public bike service in Hangzhou City which provides great convenience

Public Bike Management (30)(dfs)

連結:https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f 來源:牛客網 There is a public bike service in Hangzhou City which provi

1018 Public Bike Management - 求符合要求的最短路,並列印

程式碼參考:https://www.liuchuo.net/archives/2373 要注意的問題: 用dijk求最短路的時候,若要記錄最短路是什麼,則用pre陣列,記錄當前節點的前一個節點是什麼,我發現在初始化e陣列的時候,我習慣性的把e[i][i]=0那麼在dijk的鬆弛操作的for迴

PAT 1018 Public Bike Management

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 b