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

#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. form异步无刷新提交,提交后可以显示弹出框,否则弹出框会被刷新不见,使用 preventDefault

    出错点:确认按钮上.加onclick事件.每次点击都会追加给form追加on监听方法.累加on方法,重复提交 suppress_exception:true 阻止异常 (百度推送 jdk) 向下按 p ...

  2. Java编译成功,用java 运行class时出现错误解决方法

    java -classpath class file's address; filename

  3. AngularJS展示数据的ng-bind指令和{{}} 区别

    在AngularJS中显示模型中的数据有两种方式: 一种是使用花括号插值的方式: 1 <p>{{text}}</p> 另一种是使用基于属性的指令,叫做ng-bind: 1 &l ...

  4. 谷歌浏览器web开发教程之开始篇:使用sublime

    你的代码编辑器是主要的开发工具:你使用它去编辑和保存代码段.你可以通过学习编辑器快捷键和以及安装关键插件来好而快的写出代码. 目录 安装sublime文本编辑器 为什么使用包管理器? 安装插件 摘要 ...

  5. LightOJ 1259 Goldbach`s Conjecture 素数打表

    题目大意:求讲一个整数n分解为两个素数的方案数. 题目思路:素数打表,后遍历 1-n/2,寻找方案数,需要注意的是:C/C++中 bool类型占用一个字节,int类型占用4个字节,在素数打表中采用bo ...

  6. servlet & javabean

    1.servelet 什么是Servlet?① Servlet就是JAVA 类② Servlet是一个继承HttpServlet类的类③这个在服务器端运行,用以处理客户端的请求 Servlet相关包的 ...

  7. keyboardWillChangeFrameNotification 引发的思考 是的 思考了很久终于出结果

    func keyboardWillChangeFrameNotification(note: NSNotification) { // TODO 添加键盘弹出的事件 let userinfo = no ...

  8. android-misc-widgets四向(上下左右)抽屉bug修复版--转载

     android-misc-widgets四向(上下左右)抽屉bug修复版 2013-08-04 08:58:13 标签:bug down top panel slidingdrawer 原创作品,允 ...

  9. Java-枚举介绍

    需求:今天遇到一个问题,就是返回某些固定的int值,要用到枚举. 下面开始介绍: 无参构造方法的枚举 enum Color{ YELLOW,BLUE,RED } 解析:首先Color本身是一个枚举,里 ...

  10. 以图搜图(一):Python实现dHash算法(转)

    近期研究了一下以图搜图这个炫酷的东西.百度和谷歌都有提供以图搜图的功能,有兴趣可以找一下.当然,不是很深入.深入的话,得运用到深度学习这货.Python深度学习当然不在话下. 这个功能最核心的东西就是 ...