[pat]A1072 Gas Station
这道题的结点编号是字符串类型,处理的过程很有意思,用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的更多相关文章
- 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 ...
- A1072. Gas Station
A gas station has to be built at such a location that the minimum distance between the station and a ...
- PAT 1072 Gas Station[图论][难]
1072 Gas Station (30)(30 分) A gas station has to be built at such a location that the minimum distan ...
- PAT 1072. Gas Station
A gas station has to be built at such a location that the minimum distance between the station and a ...
- PAT_A1072#Gas Station
Source: PAT A1072 Gas Station (30 分) Description: A gas station has to be built at such a location t ...
- pat 甲级 1072. Gas Station (30)
1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...
- 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 ...
- 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 ...
- PAT甲级——1072 Gas Station
A gas station has to be built at such a location that the minimum distance between the station and a ...
随机推荐
- C# 单元测试能过,但Web项目就报错!
第一印象肯定是两个项目中各有不同的地方 背景: 公司项目采用IBatic+Castle实现 排查过程: 1.sqlmap文件是否一至,并且读取位置也要正确 2.dao.config文件要一至,读取位置 ...
- SQL中的between and
"between xx and xx "在SQL中的用法 这个大家都很熟悉,但是当问到是否包含两端的值时,就有点儿不确定了.在W3School网站上,有这样的一段话: 参考:ht ...
- 2017-2018 ACM-ICPC Latin American Regional Programming Contest
题面pdfhttps://codeforc.es/gym/101889/attachments/download/7471/statements-2017-latam-regional.pdf zyn ...
- cocoa开发Mac小试笔记
急需纠正自己的错误认识,Mac开发和iOS开发UI显示.事件交互.生命周期等存在极大的差距 首先点击事件NSButton和UIButton完全是两个不同的东西 列表UITableView和NSTabl ...
- layer开启与关闭加载层
// 开启加载层 layer.load(2, { shade: [0.6, '#fff'], content: '数据加载中...', success: function (layero) { lay ...
- zabbix客户端自动注册
1. 概述 上一篇内容<zabbix自动发现配置>,大概内容是zabbix server去扫描一个网段,把在线的主机添加到Host列表中.我们本篇内容与上篇相反,这次是Active age ...
- sed中支持变量的处理方法
1.eval sed ’s/$a/$b/’ filename2.sed "s/$a/$b/" filename3.sed ’s/’$a’/’$b’/’ filename 4.sed ...
- 栈帧 2.6. Frames 虚拟机内存模型
https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-2.html#jvms-2.6 小结: 1. https://docs.oracle. ...
- PHP之fopen wrappers模块
一.fopen wrappers模块的配置 ①.allow_url_fopen boolean //出于安全性考虑,此选项只能在 php.ini 中设置 //本选项激活了 URL 形式的 fopen ...
- [daily] 内存越界的分析与定位
valgrind 自不必说 1. Address Sanitize 很好有,只需要在gcc编译的时候,加上选项 -fsanitize=address 它的工程:https://github.com/ ...