1072 Gas Station

A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range.

Now given the map of the city and several candidate locations for the gas station, you are supposed to give the best recommendation. If there are more than one solution, output the one with the smallest average distance to all the houses. If such a solution is still not unique, output the one with the smallest index number.

Input Specification:
Each input file contains one test case. For each case, the first line contains 4 positive integers: N (≤10
​3
​​ ), the total number of houses; M (≤10), the total number of the candidate locations for the gas stations; K (≤10
​4
​​ ), the number of roads connecting the houses and the gas stations; and D
​S
​​ , the maximum service range of the gas station. It is hence assumed that all the houses are numbered from 1 to N, and all the candidate locations are numbered from G1 to GM.

Then K lines follow, each describes a road in the format

P1 P2 Dist
where P1 and P2 are the two ends of a road which can be either house numbers or gas station numbers, and Dist is the integer length of the road.

Output Specification:
For each test case, print in the first line the index number of the best location. In the next line, print the minimum and the average distances between the solution and all the houses. The numbers in a line must be separated by a space and be accurate up to 1 decimal place. If the solution does not exist, simply output No Solution.

Sample Input 1:
4 3 11 5
1 2 2
1 4 2
1 G1 4
1 G2 3
2 3 2
2 G2 1
3 4 2
3 G3 2
4 G1 3
G2 G1 1
G3 G2 2
Sample Output 1:
G1
2.0 3.3
Sample Input 2:
2 1 2 10
1 G1 9
2 G1 20
Sample Output 2:
No Solution

#include
#include
const int maxn = 1020;
const int INF = 0x3f3f3f3f;
using namespace std;
bool vis[maxn]={
     false};
int G[maxn][maxn],N,M,D,K,d[maxn];
int getid(string s){
     
	if(s[0]!='G') return stoi(s);
	else{
     
		string p=s.substr(1,s.size()-1);
		return N+stoi(p);
	}
}
void dijkstra(int s){
     
	fill(d,d+maxn,INF);
	memset(vis,false,sizeof(vis));
	d[s]=0;
	for(int i=1;i<=N+M;i++){
     
		int u=-1,min=INF;
		for(int j=1;j<=N+M;j++){
     
			if(!vis[j] && d[j] < min){
     
				u=j;
				min=d[j];
			}
		}
		if(u == -1) return;
		vis[u] =true;
		for(int i=1;i<=N+M;i++){
     
			if(d[u] + G[u][i] < d[i] && !vis[i] && G[u][i] < INF) d[i] = d[u] + G[u][i]; 
		}
	}
}
int main(){
     
	fill(G[0],G[0]+maxn*maxn,INF);
	cin>>N>>M>>K>>D;
	string s1,s2;
	int dis;
	for(int i=0;i<K;i++){
     
		cin>>s1>>s2>>dis;
		int id1=getid(s1),id2=getid(s2);
		G[id1][id2]=G[id2][id1]=dis;
	}
	double ansd=-1,ansavg=INF;
	int ansid=-1;
	for(int i=N+1;i<=N+M;i++){
     
		dijkstra(i);
		double mindis=INF,avg=0;
		for(int j=1;j<=N;j++){
     
			if(d[j] > D) {
     
				mindis=-1;
				break; 
			}
			if(d[j] < mindis) mindis=d[j];
			avg+=1.0*d[j]/N;
		}
		if(mindis == -1) continue;
		if(mindis > ansd ) {
     
			ansd=mindis;
			ansid=i;
			ansavg=avg;
		}
		else if(mindis == ansd && ansavg > avg){
     
			ansid=i;
			ansavg=avg;
		}
		
	}
	if(ansid == -1) cout<<"No Solution";
	else{
     
		cout<<"G"<<ansid-N<<endl;
		printf("%.1f %.1f",ansd,ansavg);
	}
	return 0;
} 

1.理解题意吧
就是说有n个居民房,M个候选工厂,你要从M个候选工厂种选取一个作为工厂,使其满足一下条件
1.最短距离最大
2. 如果1相同的话,要是居民房到这个工厂的平均距离最小
3. 如果2还相同,选取最小序号的工厂,其实这个不用考虑,因为你顺序遍历候选工厂,
在这个时候判断满足条件就已经是最小的序号了。

2.工厂都时G开头的,在存图的时候还要转换一下,先看转化之后的,n个居民房,m个候选工厂,候选工厂的顺序就时n+1到n+m,这样在求最小路径的时候好求,求最小路径的时候也要把候选工厂算进去,因为有些点经过候选工厂是的路径变小。
这里就用到stoi函数,可以转换字符串为整形
stoi
好久不用这个函数了…。。。。字符串的内容有的都忘完了,还有什么sscanf什么的。。用到在说吧,比如这个stoi函数

===================================================================
自我总结
首先题不是独立做出来的,因为题意没怎么读懂, 对于我来说,读题的时候,A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible.我没怎么接触过题,所以我就想最小距离怎么能最大,和我当时做另外一道dijkstra的奶牛时一摸一样,其实就是每个点最小路径最大,
和1018类似,这类题目都是在最小路径的条件下,在求第二,第三,第四权值,权值可能不恰当,在求满足要求时,往往都是先求最小路径,在求最小路径上的东西,就是dijkstra+DFS的思想
比第一次有进步了,不管题意还是心理上,还是对dijkstra的熟练程度,一开始觉得甲级英文,考的东西有难,其实难度就是在那里,只要自己一步一个脚印,学就好了。遇到不会的就理解记住,不要灰心,觉得自己不会的好多,焦虑。

你可能感兴趣的:(pat甲级,图)