[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++设计实现一个不能被继承的类
C++不同于Java,Java中被final关键字修饰的类不能被继承,C++能实现不被继承的类,但是需要自己实现. 为了使类不被继承,最好的办法是使子类不能构造父类的部分,此时子类就无法实例化整个子类 ...
- window10装机 nvem简介,针对于 联想R720系列
1.nvem格式的硬盘比较新,传统的老毛桃,大白菜,并不能有效使用. 2.对于镜像类 goust类,仍然具备 硬盘兼容性差(对于gpt,MAR,uefi并不够智能)成功率极低,容易丢失一些关键文件,造 ...
- PHP之字符串类型
PHP之存取和修改字符串中的字符 PHP中的字符串,在存储的时候是按照字节存储的.利用一个字节数组存储字符串. PHP一个字符串string就是由一系列的字符组成,其中每个字符等同于一个字节. str ...
- mysql root看不到mysql表
1.首先停止mysql服务:service mysqld stop2.加参数启动mysql:/usr/bin/mysqld_safe --skip-grant-tables & 然后就可以无任 ...
- Beanstalkd 基本概念和使用
1:什么是 Beanstalkd ? Beanstalkd 一个高性能.轻量级的分布式内存队列系统 简单来说,就是一个队列,相比于 数据库/redis 队列相比. 更专业.能完成的功能更多.就这么理解 ...
- Python开发【笔记】:aiohttp搭建简易聊天室
简易聊天室: 1.入口main.py import logging import jinja2 import aiohttp_jinja2 from aiohttp import web from a ...
- java Scanner和Random的Demo
import java.util.Scanner;//导入包 import java.util.Random; public class ScannerDemo{ public static void ...
- Linux内核如何装载和启动一个可执行程序(转)
原文:http://www.cnblogs.com/petede/p/5351696.html 实验七:Linux内核如何装载和启动一个可执行程序 姓名:李冬辉 学号:20133201 注: 原创作品 ...
- postgresSQL主从流复制安装
命令行运维: https://blog.csdn.net/zhangzeyuaaa/article/details/77941039 安装流程: 先准备类库: yum -y install readl ...
- 跨域的问题(nginx解决才是王道)
跨域分为两类:一时get跨域,而是post跨域.常见的是三种: 一种是jsonp, $.ajax({ url: "http://127.0.0.1/~chenjiebin/mycode/ph ...