PAT (Advanced Level) 1072. Gas Station (30)

枚举一下选的位置,每次算一下就可以了。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;

const int INF=0x7FFFFFFF;
const int maxn=1000+100;
int n,m,k,ds;
struct Edge
{
    int u,v;
    int dis;
}e[20*maxn];
vector<int>g[maxn];
int tot;
int flag[maxn],dis[maxn];
double ans1,ans2;
int ans;
int MIN;

int get(char *s)
{
    int num=0;
    if(s[0]!=‘G‘)
    {
        for(int i=0;s[i];i++)
            num=num*10+s[i]-‘0‘;
    }
    else
    {
        for(int i=1;s[i];i++)
            num=num*10+s[i]-‘0‘;
        num=num+n;
    }
    return num;
}

void SPFA(int s)
{
    queue<int>Q;
    memset(flag,0,sizeof flag);
    for(int i=0;i<=n+m;i++) dis[i]=INF;
    Q.push(s); flag[s]=1; dis[s]=0;
    while(!Q.empty())
    {
        int head=Q.front(); Q.pop(); flag[head]=0;
        for(int i=0;i<g[head].size();i++)
        {
            int id=g[head][i];
            if(dis[head]+e[id].dis<dis[e[id].v])
            {
                dis[e[id].v]=dis[head]+e[id].dis;
                if(flag[e[id].v]==0)
                {
                    flag[e[id].v]=1;
                    Q.push(e[id].v);
                }
            }
        }
    }
}

int main()
{
    scanf("%d%d%d%d",&n,&m,&k,&ds);

    tot=0;
    for(int i=1;i<=k;i++)
    {
        char u[5],v[5]; int dis;
        scanf("%s%s%d",u,v,&dis);
        e[tot].u=get(u),e[tot].v=get(v);
        e[tot].dis=dis;
        g[get(u)].push_back(tot),tot++;

        e[tot].u=get(v),e[tot].v=get(u);
        e[tot].dis=dis;
        g[get(v)].push_back(tot),tot++;
    }

    ans1=1.0*INF;
    ans2=1.0*INF;
    MIN=0;

    for(int i=n+1;i<=n+m;i++)
    {
        SPFA(i);
        int sum=0,MIN_NOW=INF;
        for(int j=1;j<=n;j++)
        {
            if(dis[j]>ds) {sum=-1;break;}
            else
            {
                sum=sum+dis[j];
                MIN_NOW=min(MIN_NOW,dis[j]);
            }
        }
        if(sum==-1) continue;

        if(MIN_NOW>MIN)
        {
            MIN=MIN_NOW;
            ans2=1.0*sum/n;
            ans1=1.0*MIN;
            ans=i;
        }

        else if(MIN_NOW==MIN&&1.0*sum/n<ans2)
        {
            ans2=1.0*sum/n;
            ans=i;
        }
    }
    if(MIN==0) printf("No Solution\n");
    else
    {
        printf("G%d\n",ans-n);
        printf("%.1lf %.1lf\n",ans1,ans2);
    }
    return 0;
}
时间: 2024-12-16 10:34:30

PAT (Advanced Level) 1072. Gas Station (30)的相关文章

PAT 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 o

1072. Gas Station (30)

先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!).如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 选 编号小的. 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas station has to be built at such a location that the minimum distance

1072 Gas Station (30)(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

PAT甲题题解-1072. Gas Station (30)-dijkstra最短路

题意:从m个加油站里面选取1个站点,使得其离住宅的最近距离mindis尽可能地远,并且离所有住宅的距离都在服务范围ds之内.如果有很多相同mindis的加油站,输出距所有住宅平均距离最小的那个.如果平均值还是一样,就输出按照顺序排列加油站编号最小的. 分析:加油站之间也是彼此有路连接的,所以最短路径计算的时候也要把加油站算上,是双向边,所以边的数组大小得开两倍.加油站编号记为n+1~n+m,对这些点用dijkstra计算最短路径即可,这里顺便在dijkstra中进行了剪枝处理,不剪枝也没事. #

PAT (Advanced Level) 1026. Table Tennis (30)

情况比较多的模拟题. 交了50发的样子才AC......AC之后我的天空星星都亮了. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<vector> using namespace std; struct

浙大 PAT Advanced level 1026. Table Tennis (30)

A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the smallest number. If all the

PAT (Advanced Level) 1080. Graduate Admission (30)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std;

PAT (Advanced Level) 1004. Counting Leaves (30)

简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<vector> using namespace std; const int maxn=100+10; vector<int>g[maxn]; int n,m; int ans[maxn]; int root; i

PAT (Advanced Level) 1022. Digital Library (30)

简单模拟题. 写的时候注意一些小优化,小心TLE. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<vector> using namespace std; struct X { string id; c