这道题的结点编号是字符串类型,处理的过程很有意思,用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. 如何迁移完整SQL数据库到另外一台服务器

    如何迁移完整SQL数据库到另外一台服务器: https://jingyan.baidu.com/album/9f7e7ec080d1b36f28155422.html?picindex=1

  2. jQuery 选择器 筛选器 样式操作 文本操作 属性操作 文档处理 事件 动画效果 插件 each、data、Ajax

    jQuery jQuery介绍 1.jQuery是一个轻量级的.兼容多浏览器的JavaScript库. 2.jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方 ...

  3. [No0000C0]百度网盘真实地址解析(不用下载百度网盘)20170301

    一:如果是别人分享的,就保存到自己的网盘,然后再分享出去:如果本身自己的,也是要分享出去(下面提供的代码,不可以在这里直接使用,没用的,必须分享出去) 二:必须是  创建公开链接,私密链接不行(试过了 ...

  4. PHP之对象类型

    PHP之object对象 对象初始化 要创建一个新的对象object,使用new语句实例化一个类: 转化为对象 如果讲一个对象转化成对象,它将不会有任何变化.如果其它任何类型的值被转化成对象,将会创建 ...

  5. Copycat - 状态

    Member.Status status的变迁是源于heartbeat heartbeat,append空的entries /** * Triggers a heartbeat to a majori ...

  6. Cocos 2d TestCPP 学习

    Cocos 2d testcpp包含了大量的demo, 对于新手学习cocos引擎具有非常大的帮助.因为接下来的开发项目有可能会用到该引擎,所以希望可以利用自己的业余时间提前熟悉起来.该篇文章会记录自 ...

  7. openssh源码分析笔记

    1.客户端保活: options.client_alive_interval options.client_alive_count_max 在wait_until_can_do_something() ...

  8. fopen 的使用

    1, fopen() 的使用,在linux环境下,我们可以使用man fopen查看使用说明: #include <stdio.h> FILE *fopen(const char *pat ...

  9. Loadrunner回放脚本时报错Action.c(41): Error -27979: Requested form not found [MsgId: MERR-27979]

    解决方法 打开录制选项配置对话框进行设置,在“Recording Options”的“Internet Protocol”选项里的“Recording”中选择“Recording Level”为“HT ...

  10. DNS搜索

    dig(Domain Information Groper) dig @dnsserver name querytype 如果你设置的dnsserver是一个域名,那么dig会首先通过默认的上连DNS ...