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 (<= 103), the total number of houses; M (<= 10), the total number of the candidate locations for the gas stations; K (<= 104), the number of roads connecting the houses and the gas stations; and DS, 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
#include
#include
#define M 1050
#define INF 1<<29
using namespace std;
int map_length[M][M];
int flag=0;
int len[M];
int vis[M];
int n,m,k,d;
double   min_avg =INF;
double   min_d=0;
int min_gi=0;
typedef pair<int,int > P;
void  dijstra(int s)
{
    double  sum=0;
    for(int i=0;i<=n+m;i++)
    {
           len[i]=INF;vis[i]=0;
    }
    len[s]=0;
    priority_queue vector 

,greater

> que; que.push({len[s],s}); while(que.size()) { P p=que.top(); que.pop(); int num=p.second; if (vis[num]==1) continue; vis[num]=1; for (int i=1;i<=n+m;i++) { if (map_length[i][num]!=INF&&len[i]>len[num]+map_length[i][num]) { len[i]=len[num]+map_length[i][num]; que.push({len[i],i}); } } } double min_dis=INF; for (int t=1;t<=n;t++) { if (len[t]>d) return ; sum+=1.0*len[t]; if (min_dis>len[t]) min_dis=len[t]; } double avg=1.0*sum/n; if (min_dis>min_d) { min_avg=avg; min_d=min_dis; min_gi=s; flag=1; } else if (min_dis==min_d&&min_avg>avg) { min_avg=avg; min_d=min_dis; min_gi=s; flag=1; } } int toint(char s[]){ if(s[0]=='G'){ int sum=0; for(int i=1;i<strlen(s);i++) sum=sum*10+s[i]-'0'; return sum+n; } int sum=0; for(int i=0;i<strlen(s);i++) sum=sum*10+s[i]-'0'; return sum; } int main() { cin>>n>>m>>k>>d; char p1[4],p2[4]; int dis,a,b; for (int i=0;ifor(int j=0;jfor (int i=0;icin>>p1>>p2>>dis; a=toint(p1); b=toint(p2); map_length[a][b]=map_length[b][a]=dis; } for (int i=1;i<=m;i++) dijstra(n+i); if(flag==1) printf("G%d\n%.1f %.1f",min_gi-n,1.0*min_d,min_avg); else printf("No Solution"); return 0; }

//
最后一点没过,实在找不出bug。放弃了

#include
#include
#include
#include
#define M 1050
#define INF 1<<29
using namespace std;
int map_length[M][M];
int flag=0;
int len[M];
int vis[M];
int n,m,k,d;
double   min_avg =INF;
double   min_d=0;
int min_gi=0;

void  dijstra(int s)
{
    double  sum=0;
    for(int i=0;i<1050;i++)
        len[i]=INF;
    len[s]=0;
    for (int i=0;i<1050;i++)
        vis[i]=0;

    queue <int > q;
    q.push(s);
    while(q.size())
    {
        int num=q.front();
        q.pop();
        if (vis[num]==1) continue;
        vis[num]=1;
        for (int i=1;i<=n+m;i++)
        {
            if (map_length[i][num]!=INF)
            {
                if (len[i]>map_length[i][num]+len[num])
                    len[i]=map_length[i][num]+len[num];
                q.push(i);
            }
        }
    }
    double  min_dis=INF;
    for (int t=1;t<=n;t++)
    {
        if (len[t]>d)
            return ;
        sum+=1.0*len[t];
        if (min_dis>len[t])
            min_dis=len[t];
    }
    double avg=1.0*sum/n;
     if (min_dis>min_d)
        {
             min_avg=avg;
            min_d=min_dis;
            min_gi=s;
            flag=1;
        }
         else if (min_dis==min_d&&min_avg>avg)
        {
             min_avg=avg;
            min_d=min_dis;
            min_gi=s;
            flag=1;
        }

}
int toint(char s[]){
    if(s[0]=='G'){
        int sum=0;
        for(int i=1;i<strlen(s);i++)
            sum=sum*10+s[i]-'0';
        return sum+n;
    }
    int sum=0;
    for(int i=0;i<strlen(s);i++)
        sum=sum*10+s[i]-'0';
    return sum;
}
int main()
{
    cin>>n>>m>>k>>d;
    char p1[4],p2[4];
    int dis,a,b;
    for (int i=0;ifor(int j=0;jfor (int i=0;icin>>p1>>p2>>dis;
        a=toint(p1);
        b=toint(p2);
        map_length[a][b]=map_length[b][a]=dis;
    }
    for (int i=1;i<=m;i++)
    dijstra(n+i);
    if(flag==1)
        printf("G%d\n%.1f %.1f",min_gi-n,1.0*min_d,min_avg);
    else
        printf("No Solution");
    return 0;
}

你可能感兴趣的:(PAT,最短路径算法,dijkstra算法)