垃圾箱分布

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

大家倒垃圾的时候,都希望垃圾箱距离自己比较近,但是谁都不愿意守着垃圾箱住。所以垃圾箱的位置必须选在到所有居民点的最短距离最长的地方,同时还要保证每个居民点都在距离它一个不太远的范围内。

现给定一个居民区的地图,以及若干垃圾箱的候选地点,请你推荐最合适的地点。如果解不唯一,则输出到所有居民点的平均距离最短的那个解。如果这样的解还是不唯一,则输出编号最小的地点。

输入格式:

输入第一行给出4个正整数:N(<= 103)是居民点的个数;M(<= 10)是垃圾箱候选地点的个数;K(<= 104)是居民点和垃圾箱候选地点之间的道路的条数;DS是居民点与垃圾箱之间不能超过的最大距离。所有的居民点从1到N编号,所有的垃圾箱候选地点从G1到GM编号。

随后K行,每行按下列格式描述一条道路:
P1 P2 Dist
其中P1和P2是道路两端点的编号,端点可以是居民点,也可以是垃圾箱候选点。Dist是道路的长度,是一个正整数。

输出格式:

首先在第一行输出最佳候选地点的编号。然后在第二行输出该地点到所有居民点的最小距离和平均距离。数字间以空格分隔,保留小数点后1位。如果解不存在,则输出“No Solution”。

输入样例1:

4 3 11 5
1 2 2
1 4 2
1 G1 4
1 G2 3
2 3 2
2 G2 1
3 4 2
3 G3 2
4 G1 3
G2 G1 1
G3 G2 2

输出样例1:

G1
2.0 3.3

输入样例2:

2 1 2 10
1 G1 9
2 G1 20

输出样例2:

No Solution

详见代码,dijstra

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int n,k,m,ds;
int e[][];
bool v[];
int d[];
int kk=;
struct node
{
int bh;
double p;
int h;
int zd;
}a[];
bool cmp(node x,node y)
{
if(x.zd==y.zd)
{
if(x.p==y.p)
return x.bh<y.bh;
else return x.p<y.p;
}
else return x.zd>y.zd;//最短距离要最长
}
void dijstra(int s)
{
int i;
memset(v,,sizeof(v));
v[s]=;
int zd=inf;
for(i=;i<=n+m;i++)
{
d[i]=e[s][i];
if(!v[i]&&i<=n&&d[i]<zd)//条件要写全
zd=d[i];
}
while()
{
int k=-;
int mm=inf;
for(i=;i<=n+m;i++)
{
if(!v[i]&&d[i]<mm)
{
k=i;
mm=d[i];
}
}
if(k==-) break;
v[k]=;
for(i=;i<=n+m;i++)
{
if(d[i]>d[k]+e[k][i])
{
d[i]=d[k]+e[k][i];
if(!v[i]&&i<=n&&d[i]<zd)
zd=d[i];//把最短的距离找出来
}
}
}
int ss=;
for(i=;i<=n;i++)
{
if(d[i]>ds) break;//最短路径超出范围的不要
else ss+=d[i];
}
if(i==n+)
{
a[kk].bh =s-n;
a[kk].h=ss;
a[kk].zd=zd;
a[kk++].p=ss*1.0/n;
}
} int main()
{
cin>>n>>m>>k>>ds;
int i,j;
for(i=;i<=n+m;i++)
for(j=;j<=n+m;j++)
{
if(i==j) e[i][j]=;
else e[i][j]=inf;
}
for(i=;i<=k;i++)
{
string a,b;//G的处理
int z;
cin>>a>>b>>z;
int x=,y=;
if(a[]!='G')
{
int l=a.length ();
for(int i=;i<l;i++)
{
x=x*+a[i]-'';
}
}
else
{
int l=a.length ();
for(int i=;i<l;i++)
{
x=x*+a[i]-'';
}
x+=n;
} if(b[]!='G')
{
int l=b.length ();
for(int i=;i<l;i++)
{
y=y*+b[i]-'';
}
}
else
{
int l=b.length ();
for(int i=;i<l;i++)
{
y=y*+b[i]-'';
}
y+=n;
}
if(e[x][y]>z)
{
e[x][y]=z;
e[y][x]=z;
}
}
for(int i=n+;i<=m+n;i++)//遍历每一个垃圾箱
{
dijstra(i);
}
if(k==) cout<<"No Solution";
else
{
sort(a+,a+kk,cmp);//排个序,是kk
if(a[].bh==) cout<<"No Solution";
else
{
cout<<"G"<<a[].bh<<endl;
printf("%.1lf",a[].zd*1.0);cout<<" ";
printf("%.1lf",a[].p);
}
} return ;
}

PAT 垃圾箱分布(30分)dijstra的更多相关文章

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

  2. PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)

    1030 Travel Plan (30 分)   A traveler's map gives the distances between cities along the highways, to ...

  3. PAT 甲级 1018 Public Bike Management (30 分)(dijstra+dfs,dfs记录路径,做了两天)

    1018 Public Bike Management (30 分)   There is a public bike service in Hangzhou City which provides ...

  4. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  5. PAT A1127 ZigZagging on a Tree (30 分)——二叉树,建树,层序遍历

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  6. 【PAT】1053 Path of Equal Weight(30 分)

    1053 Path of Equal Weight(30 分) Given a non-empty tree with root R, and with weight W​i​​ assigned t ...

  7. 【PAT】1091 Acute Stroke(30 分)

    1091 Acute Stroke(30 分) One important factor to identify acute stroke (急性脑卒中) is the volume of the s ...

  8. [PAT] 1147 Heaps(30 分)

    1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...

  9. [PAT] 1143 Lowest Common Ancestor(30 分)

    1143 Lowest Common Ancestor(30 分)The lowest common ancestor (LCA) of two nodes U and V in a tree is ...

随机推荐

  1. 未能将网站配置为使用ASP.NET4.X 解决方法

    WIN 10系统安装Visual Studio 2012新建ASP.NET MVC 4 WEB 应用程序出错 有些图片是网上截取而来,之前光顾着处理问题而忘记截图了,提示的ASP.net 版本有些不同 ...

  2. 由于ptrace.h文件导致的内核编译出错的解决方法

    arch/x86/kernel/ptrace.c:1472:17: error: conflicting types for 'syscall_trace_enter'  In file includ ...

  3. java连接SQL数据库(JDBC)相关设置

    2016-06-14 一.SQL server中的相关设置(以sql server 2012 版本为例) 建立一个SQL server 身份认证的服务器登录名 首先启动SQL客户端,以windows身 ...

  4. UVA 11029 || Lightoj 1282 Leading and Trailing 数学

    Leading and Trailing You are given two integers: n and k, your task is to find the most significant ...

  5. JAVA中代码前加@有什么作用

    严格说@符号并未在java 语言中作为操作符使用.而是在其他附属技术中使用比如1.javadoc 文档关键字/***  @author*  @version* @since* @see*/用于自动生成 ...

  6. matplotlib两种画散点图的方式

    对于matplotlib.pyplot( as plt ) 先输入主体数据部分: import numpy as np import matplotlib.pyplot as plt X_train ...

  7. Intel微处理结构.docx

    1. 段寄存器 CS(代码段),代码段是一个存储器区域,这里保存微处理器使用的代码(程序和过程).代码段寄存器定义了存放代码的存储器段的起始地址.在实模式下工作时,它定义一个64KB存储器段的起始地址 ...

  8. 178. Rank Scores

    问题描述 解决方案 select sc.Score, (select count(*) from (select distinct Score from Scores ) ds where ds.Sc ...

  9. WPF中的事件及冒泡事件和隧道事件(预览事件)的区别

    WPF快速指导10:WPF中的事件及冒泡事件和隧道事件(预览事件)的区别   WPF快速指导10:WPF中的事件及冒泡事件和隧道事件(预览事件)的区别 本文摘要: 1:什么是路由事件: 2:中断事件路 ...

  10. 有些事情不要强求 PS:日常收集语录

    有些事情不要强求 1.有些事,我们明知道是错的,也要去坚持,因为不甘心:有些人,我们明知道是爱的,也要去放弃,因为没结局:有时候,我们明知道没路了,却还在前行,因为习惯了. 2.以为蒙上了眼睛,就可以 ...