这道题的结点编号是字符串类型,处理的过程很有意思,用getID将house和GasStation进行区分

#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int maxn = ;
int G[maxn][maxn];
int d[maxn];
int n, m, k, ds;
bool vis[maxn] = {false};
double optSum = INF;
double optDis = ;
int opt = -;
int getID(string str)
{
int ID = ;
int i = ;
int len = str.length();
while (i < len)
{
if (str[i] != 'G')
{
ID = * ID + str[i] - '';
}
i++;
}
if (str[] != 'G')
{
return ID;
}
else
return ID + n;
}
void dj(int s)
{
d[s] = ;
int i;
for (i = ; i < n + m; i++)
{
int u = -, MIN = INF;
int j;
for (j = ; j <= n + m; j++)
{
if (vis[j]==false&&d[j]<MIN)
{
u = j;
MIN = d[j];
}
}
if (u == -)
return;
vis[u] = true;
int v;
for (v = ; v <= n + m; v++)
{
if (vis[v] == false&&G[u][v]!=INF)
{
if (d[v] > d[u] + G[u][v])
{
d[v] = d[u] + G[u][v];
}
}
}
}
}
int main()
{
scanf("%d%d%d%d", &n, &m, &k, &ds);
int i = ;
fill(G[], G[] + maxn*maxn, INF);
fill(d, d + maxn, INF);
for (i = ; i < k; i++)
{
string str1, str2;
cin >> str1 >> str2;
int st, ed;
st = getID(str1);
ed = getID(str2);
scanf("%d", &G[st][ed]);
G[ed][st] = G[st][ed];
}
for (i = ; i <= m; i++)
{
memset(vis, false, sizeof(vis));
fill(d, d + maxn, INF);
dj(n + i);//松弛完成
int j;
double sum = ;
double minDis = INF;
int flag = ;
for (j = ; j <= n; j++)//遍历n个house
{
if (d[j] <=ds)//必须能覆盖到位
{
sum += d[j];
if (minDis > d[j])
{
minDis = d[j];
}
}
else
{
flag = ;
break;
}
}
if (flag == )
{
if (minDis > optDis)
{
optDis = minDis;
optSum = sum;
opt = n+i;
}
else if (minDis == optDis&&sum < optSum)
{
optSum = sum;
opt = n + i;
}
}
}
if (opt == -)
{
printf("No Solution\n");
}
else
{
printf("G");
printf("%d\n", opt -n);
printf("%.1f ", optDis);
printf("%.1f", optSum / n);
}
}

[pat]A1072 Gas Station的更多相关文章

  1. 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 ...

  2. A1072. Gas Station

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

  3. PAT 1072 Gas Station[图论][难]

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

  4. PAT 1072. Gas Station

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

  5. PAT_A1072#Gas Station

    Source: PAT A1072 Gas Station (30 分) Description: A gas station has to be built at such a location t ...

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

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

  7. 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 ...

  8. 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 ...

  9. PAT甲级——1072 Gas Station

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

随机推荐

  1. Python与金融量化分析----金融与量化投资

    一:金融了解 金融:就是对现有资源进行重新的整合之后,进行价值和利润的等效流通. 金融工具: 股票 期货 黄金 外汇 基金 ............. 股票: 股票是股份公司发给出资人多的一种凭证,股 ...

  2. MySQL设置只读模式

    MySQL设置了主从复制,为保证数据一致性需要在从库设置只读状态 查看默认读写状态 show global variables like "%read_only%"; 设置只读 # ...

  3. PAT甲级1055 The World's Richest【排序】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805421066272768 题意: 给定n个人的名字,年龄和身价. ...

  4. Please check logcat output for more details

    Please check logcat output for more details 小米第一次可以安装,后面就不行了,研究发现 手机里面有同名的apk,直接elipse重命名 就可以了. 小米us ...

  5. 20165311 实验一 Java开发环境的熟悉

    一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:李嘉昕 学号:20165311 指导教师:娄嘉鹏 实验日期:2018年4月2日 实验时间:13:45 - 15:25 实验序号:3 实 ...

  6. python-----多线程、线程池、进程池

    import threadingimport time ###############################多线程################################------ ...

  7. PHP之引用

    php数字月份转换为英语缩写 实现数字月份到英文月份缩写的转换 英语 1 => 'Jan', January 2 => 'Feb', February 3 => 'Mar', Mar ...

  8. PLSQL游标

    静态游标:结果集已经确实(静态定义)的游标.分为隐式和显式游标 隐式游标:所有DML语句为隐式游标,通过隐式游标属性可以获取SQL语句信息: 显式游标:用户显式声明的游标,即指定结果集.当查询返回结果 ...

  9. LeetCode 104 Maximum Depth of Binary Tree 解题报告

    题目要求 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  10. 2018/04/25 基于 编译安装的 PHP7 安装 swoole 扩展

    在上一篇文章我们知道了如何去编译安装一个自己需要的 PHP 版本. 2018/04/25 PHP7的编译安装 这里还没有完,我们还需要安装我们的扩展,才算完成今天的任务. -- 下载扩展 还是官网下载 ...