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.
1.题意需要稍微费点时间理解一下,找一个加油站,到任意居民点的最小距离最大
2.算法:dijkstra or spfa 等最短路算法
floyd复杂度太高不适用本题
3.难点:加油站的输入是字符串形式,可以写个函数处理一下
G1=n+1
G2=n+2
n为居民点个数
这样做一下转化就好了
然后对每一个加油站使用一次spfa求出到所有点的最短路,对得出来的路径分析一下即可出答案
这种题目建议先画图出样例
距离可以用邻接矩阵代替,代码量会少很多

#include <bits/stdc++.h>
using namespace std;
#define FORP(i,a,b) for(int i=a;i<=b;i++)
#define mp(a,b) make_pair(a,b)
#define maxn 202000
#define debug(a) (cout<<"="<<a<<endl)
int n,l,m,k,d;
int ddd[1050][1050];
std::vector<int> v[1060];
int cal(string a){
    int ans=0;
    for(int i=0;i<a.size();i++){
        if(a[i]=='G') continue;
        ans=ans*10+a[i]-'0';
    }
    return ans;
}
int dis[1200];
bool inq[1200];
int tot;
bool ok;
int Min;
void spfa(int x){
    memset(dis,0x3f,sizeof(dis));
    memset(inq,0,sizeof(inq));
    dis[x]=0;
    inq[x]=1;
    queue<int> q;
    q.push(x);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for(int i=0;i<v[u].size();i++){
            int to=v[u][i];
            if(ddd[u][to]+dis[u]<dis[to]){
                if(!inq[to]){
                    inq[to]=1;  
                    q.push(to);
                }
                dis[to]=ddd[u][to]+dis[u];
            }
        }
        inq[u]=false;
    }
    FORP(i,1,n){
        tot+=dis[i];
        Min=min(Min,dis[i]);
        if(dis[i]>d){
            ok=false;
            return;
        }
    }
}
int main(){
    memset(ddd,0x3f,sizeof(ddd));
    ios::sync_with_stdio(false);
    cin>>n>>m>>k>>d;
    FORP(i,1,k){
        string a,b;
        cin>>a>>b;
        int dist;cin>>dist;
        int aa,bb;
        if(a[0]=='G'){
            aa=cal(a)+n;
        }
        else aa=cal(a);
        if(b[0]=='G'){
            bb=cal(b)+n;
        }
        else bb=cal(b);
        ddd[aa][bb]=dist;
        ddd[bb][aa]=dist;
        v[aa].push_back(bb);
        v[bb].push_back(aa);
    }
    int ans=0;
    int gas=-1;
    int mm=0;
    for(int i=n+1;i<=n+m;i++){
        ok=true;
        tot=0;
        Min=0x3f3f3f3f;
        spfa(i);
        if(ok){
            if(Min>mm){
                ans=tot;
                gas=i;
                mm=Min;
            }
            else if(Min==mm){
                if(tot<ans){
                    ans=tot;
                    gas=i;
                }
            }
        }
    }
    if(mm==0){
        cout<<"No Solution"<<endl;
    }
    else{
        cout<<"G"<<gas-n<<endl;
        printf("%.1lf %.1lf\n",mm*1.0,ans*1.0/n);
    }
    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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值