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

#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=+;
int n,m,k,ds;
struct Edge
{
int u,v;
int dis;
}e[*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=;
if(s[]!='G')
{
for(int i=;s[i];i++)
num=num*+s[i]-'';
}
else
{
for(int i=;s[i];i++)
num=num*+s[i]-'';
num=num+n;
}
return num;
} void SPFA(int s)
{
queue<int>Q;
memset(flag,,sizeof flag);
for(int i=;i<=n+m;i++) dis[i]=INF;
Q.push(s); flag[s]=; dis[s]=;
while(!Q.empty())
{
int head=Q.front(); Q.pop(); flag[head]=;
for(int i=;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]==)
{
flag[e[id].v]=;
Q.push(e[id].v);
}
}
}
}
} int main()
{
scanf("%d%d%d%d",&n,&m,&k,&ds); tot=;
for(int i=;i<=k;i++)
{
char u[],v[]; 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=; for(int i=n+;i<=n+m;i++)
{
SPFA(i);
int sum=,MIN_NOW=INF;
for(int j=;j<=n;j++)
{
if(dis[j]>ds) {sum=-;break;}
else
{
sum=sum+dis[j];
MIN_NOW=min(MIN_NOW,dis[j]);
}
}
if(sum==-) 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==) printf("No Solution\n");
else
{
printf("G%d\n",ans-n);
printf("%.1lf %.1lf\n",ans1,ans2);
}
return ;
}

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

  1. 【PAT甲级】1072 Gas Station (30 分)(Dijkstra)

    题意: 输入四个正整数N,M,K,D(N<=1000,M<=10,K<=10000)分别表示房屋个数,加油站个数,路径条数和加油站最远服务距离,接着输入K行每行包括一条路的两条边和距 ...

  2. 1072. Gas Station (30)【最短路dijkstra】——PAT (Advanced Level) Practise

    题目信息 1072. Gas Station (30) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B A gas station has to be built at s ...

  3. pat 甲级 1072. Gas Station (30)

    1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...

  4. PAT 甲级 1072 Gas Station (30 分)(dijstra)

    1072 Gas Station (30 分)   A gas station has to be built at such a location that the minimum distance ...

  5. PAT Advanced 1072 Gas Station (30) [Dijkstra算法]

    题目 A gas station has to be built at such a location that the minimum distance between the station an ...

  6. PAT 1072. Gas Station (30)

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  7. 1072. Gas Station (30)

    先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!).如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 ...

  8. 1072. Gas Station (30) 多源最短路

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  9. 1072 Gas Station (30)(30 分)

    A gas station has to be built at such a location that the minimum distance between the station and a ...

随机推荐

  1. Oracle小技术集锦

  2. LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  3. Centos中压缩(zip)和解压(unzip)命令

    摘自:http://liuzhichao.com/p/681.html 1.我下载了一个yasuo.zip文件,想解压缩: # unzip yasuo.zip 2.我当前目录下有abc1.zip,ab ...

  4. Mac下截图快捷键

    Cmd+Shift+3:全屏截图:截取整个屏幕,保存截图至桌面文件夹.Cmd+Shift+4:区域截图:鼠标光标变成带坐标的小十字,通过拖拽截取特定区域,保存截图至桌面文件夹.Cmd+Shift+4 ...

  5. 【读书笔记】C Primer Plus ch.15位运算 示例程序15.1 整数转换成二进制字符串

    正文: https://www.zybuluo.com/RayChen/note/595213

  6. Tomcat服务器顶层结构和启动过程【转】

    号外:2016 最流行的是哪一种 Java 应用服务器呢? 通过从部署的 1240 个 JVM 中得到的数据,我们能够确定出现了 862 个容器供应商,或者说是占到了运行环境的 70% 左右.这些容器 ...

  7. PHP的几个常用加密函数【转载】

    转自 https://jellybool.com/post/php-encrypt-functions 在网站的开发过程中,常常需要对部分数据(如用户密码)进行加密,本文主要介绍PHP的几个常见的加密 ...

  8. 禁止root远程登录 sshd问题:A protocol error occurred. Change of username or service not allowed

    在研究Linux安全的时候遇到一个问题,原本打算修改linux直接远程root登陆,修改为sshd的配置文件后 Nano /etc/ssh/sshd_config 把#PermitRootLogin ...

  9. SQL中的左连接与右连接有什么区别,点解返回值会不同?(转)

    例子,相信你一看就明白,不需要多说 A表(a1,b1,c1) B表(a2,b2) a1 b1 c1 a2 b2 01 数学 95 01 张三 02 语文 90 02 李四 03 英语 80 04 王五 ...

  10. 在PL/SQL/sqlplus客户端 中如何让程序暂停几秒钟

    1. how to check procedure exist: SQL> conn oper/oper123Connected.SQL> desc dbms_lock;PROCEDURE ...