Dijkstra_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

题目大意

有n个村庄,m个加油站预选位置,并且每个加油站都有服务范围,要求每个村庄都在加油站的服务范围内,并且要求寻找离加油站最近距离最大的一个位置 ,如果最大距离相等,找到达村庄平均距离最小的加油站位置,去过前面两个都想等,则找编号最小的一个加油站位置

思路

因为加油站位置最多10个,所以可以直接枚举加油站位置,一次判断是否满足村庄都在服务范围内,并且找到最符合要求的村庄

题最后一个样例总是过不起,用的是getchar依次判断,后来把输入换成string就对了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#define INF 0x3f3f3f3f

using namespace std;
const double pi = acos(-1.0);
const int maxn = 10005;
int n,m,k,d;
int head[maxn*2],u[maxn*2],v[maxn*2],w[maxn*2],next1[maxn*2],dis[maxn];
bool vis[maxn];
int num = 0;
struct Node
{
    int p,cost;
    bool operator<(const Node &a)const
    {
        return cost > a.cost;
    }
};
void Add_edge(int x,int y,int z)
{
    u[num] = x;v[num] = y,w[num] = z;next1[num] = head[x],head[x] = num++;
    u[num] = y;v[num] = x;w[num] = z;next1[num] = head[y],head[y] = num++;
}
int Toint(string s)
{
    int sum=0;
    int len=s.length();
    for(int i=0;i<len;i++){
        if(s[i]=='G') continue;
        sum=sum*10+s[i]-'0';
    }
    if(s[0]=='G') return sum+n;
    return sum;
}
void Input()
{
    for(int i =0 ;i < k;i ++)
    {
    string x,y,z;
    cin>>x>>y>>z;
    Add_edge(toint(x),toint(y),toint(z));
    }
}
void Dijkstra(int start)
{
    memset(vis,false,sizeof(vis));
    fill(dis,dis+maxn,INF);
    Node node,temp;node.cost = 0;node.p = start;
    dis[start] = 0;
    priority_queue<Node> que;
    que.push(node);
    while(!que.empty())
    {
        node = que.top();que.pop();
        if(vis[node.p]) continue;
        vis[node.p] = true;
        int kk = head[node.p];
        while(kk != -1)
        {
            if(dis[v[kk]] > dis[u[kk]] + w[kk])
            {
                dis[v[kk]] = dis[u[kk]] + w[kk];
                if(!vis[v[kk]])
                {
                    temp.p = v[kk];
                    temp.cost = dis[v[kk]];
                    que.push(temp);
                }
            }
            kk = next1[kk];
        }
    }
}
int main()
{
    double minnum = 0,average = INF;
    int ans;
    memset(head,-1,sizeof(head));
    scanf("%d%d%d%d",&n,&m,&k,&d);
//    getchar();
    Input();
    for(int i =n+1 ;i <= n+m;i ++)
    {
        Dijkstra(i);
        double all_cost = 0,min_cost_i = INF;
        bool flag = true;
        for(int j =1 ;j <= n;j ++)
        {
//            cout <<i <<"到达"<<j<<"的距离为"<<dis[j]<<endl;
            if(dis[j] < min_cost_i)
                min_cost_i = dis[j];
            if(dis[j] > d)
            {
                flag = false;
                break;
            }
            all_cost += 1.0*dis[j]/n;
        }
        if(!flag) continue;
        if(minnum < min_cost_i || (minnum == min_cost_i && all_cost < average))
            average = all_cost,ans = i,minnum = min_cost_i;
    }
    if(average == INF)
        printf("No Solution\n");
    else
    {
        printf("G%d\n",ans-n);
        printf("%.1lf %.1lf\n",minnum,average);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/li1615882553/article/details/85269269