1072 Gas Station(30 分)

1072 Gas Station(30 分)

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

实在很无奈了 ,因为输入的处理问题,最后一个测试点 运行时错误或者wa    自己在G这块没有好好考虑清楚难受

还有就是........ 跑M次的dij 求出来即可

还有就是题目我真的看不太懂  本以为只要输出avg最小的,相同的话id最小就可以了并且在范围内

结果发现还要先判断输出最小的距离最大。。。。这边我真的没有想到.....一直在纠结是不是自己的dj跑错了

好吧只能说要好好审题

代码

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
int maze[1025][1025];
int di[1025],vis[1025]; 
int n, m, k,ds,dis;
struct node
{
	double minn,avg;
	int id;
}s[15];
vector<node>vec;
bool cmp(node a, node b)
{
	if(a.minn != b.minn)
		return a.minn > b.minn; //选出居住点中距离最大的 
	if (a.avg != b.avg) 
		return a.avg < b.avg;
	else  
		return a.id < b.id;
}
void dij(int x)
{
	fill(vis,vis + 1025,0); 
	fill(di,di+1025,INF);
	vis[x] = 1;
	for(int i = 1; i <= n + m; i++) //最后的那些Gi不用管 
		di[i] = maze[x][i];	
	
	for(int i = 1; i <= n + m; i++)
	{
		//寻找最小的
		int minn = INF,u = 0;
		for(int j = 1; j <= n + m; j++ )
		{
			if(vis[j] == 0 && di[j] < minn)	
			{
				minn = di[j];
				u = j;
			} 
		}
		if(minn == INF)
			break;
		vis[u] = 1;
		for(int j = 1; j <= n + m ; j++)
		{
			if(!vis[j] && di[u] + maze[u][j] < di[j])
			{
				di[j] = di[u] + maze[u][j];
			}
		}
	} 
	return;
}
int main()
{
	char s1[5],s2[5];
	scanf("%d %d %d %d",&n,&m,&k,&ds);
	for(int i = 1; i <= n+m; i++ )
	{
		for(int j = 1; j <= n+m; j++)
			maze[i][j] = INF;
	}
	for(int i = 1; i <= k; i++)
	{
	    char p1[5],p2[5];
	    int dis;
	    int a = 0,b = 0;
	    scanf("%s %s %d",p1,p2,&dis);
	    if(p1[0] != 'G')
	    {
	      int j = 0;
	      while(p1[j] != '\0')
	        a = a * 10 + (int)(p1[j++] - '0');
	    }
	    else
	    {
	      int j = 1;
	      while(p1[j] != '\0')
	        a = a * 10 + (int)(p1[j++] - '0');
	      a = n + a;
	    }
	    if(p2[0] != 'G')
	    {
	      int j = 0;
	      while(p2[j] != '\0')
	        b = b * 10 + (int)(p2[j++] - '0');
	    }
	    else
	    {
	      int j = 1;
	      while(p2[j] != '\0')
	        b = b * 10 + (int)(p2[j++] - '0');
	      b = b + n;
	    }
	    maze[a][b] = maze[b][a] = dis;
	} //包括如果有重边的情况都处理了

	for(int i = 1; i <= m; i++)//然后现在跑M次的dij
	{
		dij(i + n);	 //表示是Gi
		int u;
		double sum = 0 ;
		int minn = INF;
		for( u = 1; u <= n; u++)
		{
			if(di[u] > ds)
				break;
			sum += di[u];
			minn = min(minn, di[u]);
		}
		if (u == n + 1)
		{
			node kk;
			kk.avg = double(sum * 1.0 / n);
			kk.minn = double(minn);
			kk.id = i;	
			vec.push_back(kk);		
		}
	} 
	if(vec.size() == 0)
	{
		printf("No Solution\n");
		return 0;
	}
	sort(vec.begin(),vec.end(),cmp);
	printf("G%d\n",vec[0].id);
	printf("%.1lf %.1lf",vec[0].minn,vec[0].avg);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用c++解决pipeline system consists of N transfer station, some of which are connected by pipelines. For each of M pipelines the numbers of stations A[i] and B[i], which are connected by this pipeline, and its profitability C[i] are known. A profitability of a pipeline is an amount of dollars, which will be daily yielded in taxes by transferring the gas through this pipeline. Each two stations are connected by not more than one pipeline. The system was built by Soviet engineers, who knew exactly, that the gas was transferred from Ukrainian gas fields to Siberia and not the reverse. That is why the pipelines are unidirectional, i.e. each pipeline allows gas transfer from the station number A[i] to the station number B[i] only. More over, if it is possible to transfer the gas from the station X to the station Y (perhaps, through some intermediate stations), then the reverse transfer from Y to X is impossible. It is known that the gas arrives to the starting station number S and should be dispatched to the buyers on the final station number F. The President ordered the Government to find a route (i.e. a linear sequence of stations which are connected by pipelines) to transfer the gas from the starting to the final station. A profitability of this route should be maximal. A profitability of a route is a total profitability of its pipelines. Unfortunately, the President did not consider that some pipelines ceased to exist long ago, and, as a result, the gas transfer between the starting and the final stations may appear to be impossible... Input The first line contains the integer numbers N (2 ≤ N ≤ 500) and M (0 ≤ M ≤ 124750). Each of the next M lines contains the integer numbers A[i], B[i] (1 ≤ A[i], B[i] ≤ N) and C[i] (1 ≤ C[i] ≤ 10000) for the corresponding pipeline. The last line contains the integer numbers S and F (1 ≤ S, F ≤ N; S ≠ F). Output If the desired route exists, you should output its profitability. Otherwise you should output "No solution".
05-28
这是一个经典的图论问题,可以使用Dijkstra算法或Bellman-Ford算法解决。以下是使用Dijkstra算法的步骤: 1. 定义一个数组dist,其中dist[i]表示从起点S到第i个站点的最大收益。 2. 初始化dist数组,将起点S的dist[S]赋值为0,其他点的dist[i]赋值为负无穷。 3. 定义一个优先队列pq,将起点S加入队列中。 4. 当pq非空时,取出队列中dist最小的站点u。 5. 对于每个与站点u相邻的站点v,如果通过从u到v的边可以获得更高的收益,则更新dist[v]为dist[u]+边(u,v)的收益,并将v加入队列pq中。 6. 重复步骤4和步骤5,直到队列pq为空。 7. 最终dist[F]即为最大收益。如果dist[F]为负无穷,则表示从起点S无法到达终点F,输出"No solution"。 以下是使用Dijkstra算法解决此问题的C++代码实现: ```c++ #include <iostream> #include <vector> #include <queue> using namespace std; const int INF = 0x3f3f3f3f; int main() { int n, m, s, f; cin >> n >> m; vector<vector<pair<int, int>>> g(n + 1); for (int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; g[a].push_back({b, c}); } cin >> s >> f; vector<int> dist(n + 1, -INF); dist[s] = 0; priority_queue<pair<int, int>> pq; pq.push({0, s}); while (!pq.empty()) { int u = pq.top().second; pq.pop(); for (auto p : g[u]) { int v = p.first; int w = p.second; if (dist[v] < dist[u] + w) { dist[v] = dist[u] + w; pq.push({dist[v], v}); } } } if (dist[f] == -INF) cout << "No solution" << endl; else cout << dist[f] << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值