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. 

考试的时候这句话意思没有完全理解,悲剧鸟。。。

其实就是让你找一个加油站,它到所有房子最短距离为len , 找一个加油站len的距离越大越好

加油站最多10个,以这几个加油站为原点,做dijk最短路即可

1072. Gas Station (30) 多源最短路
#include<stdio.h>



int map[1099][1099];

const int MAX=499999999;



int use[1099];

int dis[1099];

int n,m,k,ds;

void djik(int start){

    int i,j,end=n+m;

    for(i=0;i<=end;i++){

        use[i]=0;

        dis[i]=MAX;

    }dis[start]=0;



    for(i=1;i<=end;i++){

        int min=MAX,rj;

        for(j=1;j<=end;j++){

            if(use[j]==1)continue;

            if(dis[j]<min){

                min=dis[j];

                rj=j;

            }

        }use[rj]=1;



        for(j=1;j<=end;j++){

            

            if(dis[j]>dis[rj]+map[rj][j])

                dis[j]=dis[rj]+map[rj][j];

        }

    }



    for(i=1;i<=end;i++){

        if(start!=i)

        map[start][i]=dis[i];

    }

}



int main()

{



    while(scanf("%d%d%d%d",&n,&m,&k,&ds)!=EOF){

        int i,j,end,x,ri;

        end=n+m;

        

        for(i=1;i<=end;i++){

            for(j=1;j<=end;j++)map[i][j]=MAX;

        }

        char ss[9],ss2[9];

        int ll,rr,v;

        for(i=1;i<=k;i++){

            scanf("%s",ss);

            scanf("%s",ss2);

            scanf("%d",&v);

            if(ss[0]=='G'){

                if(ss[1]==0){

                    ll=ss[0]-'0'+n;

                }else if(ss[2]==0){

                    ll=ss[1]-'0'+n;

                }else{

                    ll=10+n;

                }

            }else{

                sscanf(ss,"%d",&ll);

            }

            

            if(ss2[0]=='G'){

                if(ss2[1]==0){

                    rr=ss2[0]-'0'+n;

                }else if(ss2[2]==0){

                    rr=ss2[1]-'0'+n;

                }else{

                    rr=10+n;

                }

            }else{

                sscanf(ss2,"%d",&rr);

            }

            

            map[ll][rr]=map[rr][ll]=v;

        }



        for(i=n+1;i<=end;i++){

            djik(i);

        }

        

    /*    for(x=1;x<=end;x++){

            for(i=1;i<=end;i++){

                for(j=1;j<=end;j++){

                    if(map[i][x]+map[x][j]<map[i][j])

                        map[i][j]=map[i][x]+map[x][j];

                }

            }

        }*/

        

        int tempall,all,min=0,fail=0;

        for(i=n+1;i<=end;i++){

            tempall=0;

            int tempmin=MAX;

            int ok=1;

            for(j=1;j<=n;j++){

                if(i==j)continue;

                if(map[i][j]>ds)

                    ok=0;

                if(ok==0)break;

                tempall+=map[i][j];

                

                if(tempmin>map[i][j])

                    tempmin=map[i][j];

            }

            if(ok==0){

                fail++;

                continue;

            }



            if(tempmin==min){

                if(tempall<all){

                    ri=i;

                    all=tempall;

                }

            }

            if(tempmin>min){

                min=tempmin;

                ri=i;

                all=tempall;

            }



        //        printf("G%

        //d\n",ri-n);

        //    printf("%.1lf %.1lf\n",min*1.0,all*1.0/n);

        }

        

        if(fail==m){

            printf("No Solution\n");

        }else{

            printf("G%d\n",ri-n);

            printf("%.1lf %.1lf\n",min*1.0,all*1.0/n);

        }

    }

    return 0;

}
View Code

 

 

你可能感兴趣的:(最短路)