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

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.

输入描述:

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

package xyh.pat;
import java.util.Scanner;
public class PublicBikeManagement {
	
	public static void main(String[] args) {
		//车站的最大容量
		 int c;
		//车站的数量
		 int n;
		//问题车站的编号
		 int s;
		//路线的条数
		 int m;
		//车站内的车数量
		int[]station;
		//邻接矩阵
		int[][]stationPath;
		//存放最短原路径的原点
		 int[]source;
		 int[]dist;
		 //需要移动的自行车
		 int moveNumber;
		String[]path;
		Scanner scanner=new Scanner(System.in);
		String string=scanner.nextLine();
		String[] strings=string.split(" ");
		c=Integer.parseInt(strings[0]);
		n=Integer.parseInt(strings[1]);  
		s=Integer.parseInt(strings[2]);
		m=Integer.parseInt(strings[3]);
		station=new int[n+1];
		String string2=scanner.nextLine();
		String[] strings2=string2.split(" ");
		for(int i=1;i<n+1;i++) {
			station[i]=Integer.parseInt(strings2[i-1]);
		}
		//需要加上PBMC
		stationPath=new int[n+1][n+1];
		//-1表示原点之间没有弧
		for(int i=0;i<n+1;i++) {
			for(int j=0;j<n+1;j++) {
				if(i==j) 
					stationPath[i][j]=0;
				else
				stationPath[i][j]=Integer.MAX_VALUE/2;
			}
		}
		String[]strings3=new String[m];
		for(int i=0;i<m;i++) {
			strings3[i]=scanner.nextLine();
			String[]strings4=strings3[i].split(" ");
			int v1=Integer.parseInt(strings4[0]);
			int v2=Integer.parseInt(strings4[1]);
			int v3=Integer.parseInt(strings4[2]);
			//构造图
			stationPath[v2][v1]=v3;
			stationPath[v1][v2]=v3;
		}
		//dis【i】表示从V0-v1的最短路径的长度n+1表述原点的个数
		dist=new int[n+1];
		path=new String[n+1];
		//这条路线上的总车辆
		 int[]path_bike_number=new int[n+1];
		source=new int[n+1];
		for(int i=0;i<n+1;i++) {
			dist[i]=stationPath[0][i];
			if(dist[i]!=Integer.MAX_VALUE/2) {
				path[i]=0+"->"+i;
				path_bike_number[i]+=station[i];
 			}else {
 				path[i]="";
 			}
		}
		source[0]=0;//初始化集合S
		dist[0]=0; //标记顶点v为源点
		int num=1;
		int k,i;
		while(num<n+1) {
			for(k=0,i=0;i<n+1;i++) {
				while(dist[k]==0) {
					k++;
				}
				if(dist[i]!=0&&(dist[i]<dist[k])) {
					k=i; 
				}
			}
			source[num++]=k;
			for(i=0;i<n+1;i++) {
				if(dist[i]>=dist[k]+stationPath[k][i]&&i!=k) {					
					//dist相当于存最短路径,然后一步一步被刷新。
					if(dist[i]==dist[k]+stationPath[k][i]) {
						//如果原来的路径与现在的路径相等(有多条最短路径)
						//比对车辆数量,如果小的话,就替换
						String[]p=path.clone();
						p[i]=path[k]+"->"+i;
						if(judge(station, p, i, path_bike_number, c)<judge(station, path, i, path_bike_number, c)) {
							path_bike_number[i]=path_bike_number[k]+station[i];
							path[i]=path[k]+"->"+i;
						}
					}else {
						path[i]=path[k]+"->"+i;
						path_bike_number[i]=path_bike_number[k]+station[i];
					}
					dist[i]=dist[k]+stationPath[k][i];				
				}
			}
			dist[k]=0;
		}
		int v=(path[s].split("->").length-1)*c/2-path_bike_number[s];
		int in=judge(station, path, s, path_bike_number, c);
		int out=0;
		
		if(in<0) {
			in=0;
			out=-v;
		}else {
			out=in-v;
		}
			
		System.out.println(in+" "+path[s]+" "+out);	
	}
	public static int judge(int[]station,String[] path,int s,int[] path_bike_number,int c) {
		int in=0;
		int[]station1=station.clone();
		
		String[]strings4=path[s].split("->");
		for(int j=1;j<strings4.length;j++) {
			int x=Integer.parseInt(strings4[j]);	
			if(station1[x]>c/2&&(j+1)<strings4.length) {
				int x2=Integer.parseInt(strings4[j+1]);
				station1[x2]=station1[x2]+station1[x]-c/2;
				station1[x]=c/2;
				
			}
		}
		for(int j=1;j<strings4.length;j++) {
			int x=Integer.parseInt(strings4[j]);
			if(station1[x]<c/2) {
				in=in+c/2-station1[x];
			}
		}
		return in;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_36781505/article/details/81161196