[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 ...
随机推荐
- Linux下coreseek环境安装 、mysql数据源、sphinx扩展安装及php调用
一.安装m4-1.4.13.autoconf-2.64.automake-1.11.libtool-2.2.6 下载安装m4-1.4.13.autoconf-2.64.automake-1.11.li ...
- ASP.NET MVC + EF 更新的几种方式
1.常用 db.Entry(实体).State = EntityState.Modified;db.SaveChanges(); 2.指定更新 db.Configuration.ValidateOnS ...
- d9
# 整体进度# python基础 ——38天 2个月# 数据库 —— 存储数据和信息用的,本质上和文件没有区别 1-2周 # —— 增删改查更方便了# 前端 —— 2周# 框架 —— django 2 ...
- ASP.NET Core奇遇记:无用户访问,CPU却一直100%
这是5月11日遇到的一个问题,1台1核1G阿里云Linux服务器运行着生产环境中的ASP.NET Core站点,出现CPU 100%问题. 开始以为是这台服务器负载高引起的,于是将这台服务器从负载均衡 ...
- android 平台签名
特定程序为了获取平台的权限,需要用平台签名 可以用 build/target/product 下的私钥来签名 bat代码如下: java -jar signapk.jar emt100d\platfo ...
- vmware为我们提供了三种网络工作模式,它们分别是:Bridged(桥接模式)、NAT(网络地址转换模式)、Host-Only(仅主机模式)。
原文来自http://note.youdao.com/share/web/file.html?id=236896997b6ffbaa8e0d92eacd13abbf&type=note 我怕链 ...
- Mysql 性能优化教程
Mysql 性能优化教程 目录 目录 1 背景及目标 2 Mysql 执行优化 2 认识数据索引 2 为什么使用数据索引能提高效率 2 如何理解数据索引的结构 2 优化实战范例 3 认识影响结果集 4 ...
- 再探树形dp
随着校oj终于刷进了第一页,可以不用去写那些水题了,开始认真学习自己的东西,当然包括文化课.努力.. 这道题呢是道树形dp,可看到了根本就不知道怎么写思考过程: 5min 终于看懂了题 画了样例的图把 ...
- Oracle使用par文件进行全库导入导出
expdp \"/ as sysdba\" PARFILE=/oracle/expdp/AINEWAWARD_20181005.par directory=dumpdir dump ...
- 《mongoDB》概念-数据类型
一:概念 - mongoDB 是一个面向文档的数据库,而不是关系型数据库. - 摘自<mongoDB 权威指南 第2版>第3页 二:数据类型 - null - 用于表示空值或者不存在的字段 ...