描述A city is served by a number of fire stations. Some residents have complained that the distance from their houses to the nearest station is too far, so a new station is to be built. You are to choose the location of the fire station so as to reduce the distance to the nearest station from the houses of the disgruntled residents.

The city has up to 500 intersections, connected by road segments of various lengths. No more than 20 road segments intersect at a given intersection. The location of houses and firestations alike are considered to be at intersections (the travel distance from the intersection to the actual building can be discounted). Furthermore, we assume that there is at least one house associated with every intersection. There may be more than one firestation per intersection.

输入
The first line of input contains two positive integers: f,the number of existing fire stations (f <= 100) and i, the number of intersections (i <= 500). The intersections are numbered from 1 to i consecutively. f lines follow; each contains the intersection number at which an existing fire station is found. A number of lines follow, each containing three positive integers: the number of an intersection, the number of a different intersection, and the length of the road segment connecting the intersections. All road segments are two-way (at least as far as fire engines are concerned), and there will exist a route between any pair of intersections.

Subsequent test cases are separated with a single blank line.

The number of test cases are less than 200.

输出
You are to output a single integer for each test case: the lowest intersection number at which a new fire station should be built so as to minimize the maximum distance from any intersection to the nearest fire station.
样例输入
1 6
2
1 2 10
2 3 10
3 4 10
4 5 10
5 6 10
6 1 10
样例输出
5
来源
University of Waterloo Local Contest 199
上传者
张云聪

无奈...自己的程序POJ可以过....理工的过不去...POJ(可以用Floyd...水过)

标程代码:

 1
 2 #include <iostream>
 3 #include <stdio.h>
 4 #include <string.h>
 5 #include <queue>
 6 using namespace std;
 7 #define maxN 510
 8 #define MAX 0x0fffffff
 9 int max(int x,int y){return x>y?x:y;}
10 struct point{
11     int v,nex,w;
12 }po[maxN*maxN];
13 int num,N,M,head[maxN],disY[maxN],disX[maxN],key[maxN],flag[maxN];
14 bool vis[maxN];
15 void insert(int u,int v,int w)
16 {
17     po[num].v=v;
18     po[num].w=w;
19     po[num].nex=head[u];
20     head[u]=num++;
21 }
22 void spfa(int soure,int* dis)
23 {
24     memset(vis,false,sizeof(vis));
25     queue<int>q;
26     q.push(soure);
27     vis[soure]=true;dis[soure]=0;
28     while(!q.empty())
29     {
30         int u=q.front();q.pop();vis[u]=false;
31         for(int i=head[u];i!=-1;i=po[i].nex)
32             if(dis[po[i].v]>dis[u]+po[i].w){
33                 dis[po[i].v]=dis[u]+po[i].w;
34                 if(!vis[po[i].v]){
35                     q.push(po[i].v);
36                     vis[po[i].v]=true;
37                 }
38             }
39     }
40 }
41 int main()
42 {
43     //freopen("1.txt","r",stdin);
44     char s[30];
45     while(~scanf("%d%d",&M,&N))
46     {
47         getchar();
48         int i,j,u,v,w,k=1,minn=MAX;
49         num=0;
50         memset(head,-1,sizeof(head));
51         memset(flag,0,sizeof(flag));
52         for(i=1;i<=M;i++){scanf("%d",&key[i]);getchar();}
53         if(N==1){printf("1\n");continue;}
54         while(gets(s)!=NULL&&strlen(s)){sscanf(s,"%d%d%d",&u,&v,&w);insert(u,v,w);insert(v,u,w);}
55         for(i=0;i<=N;i++)disX[i]=MAX;
56         for(i=1;i<=M;i++)spfa(key[i],disX);
57         for(j=1;j<=N;j++){
58             int maxx=0;
59             if(disX[j]==0)continue;
60             for(i=1;i<=N;i++)disY[i]=disX[i];
61             spfa(j,disY);
62             for(i=1;i<=N;i++)maxx=max(maxx,disY[i]);
63             if(minn>maxx||maxx==0){k=j;minn=maxx;}
64         }
65         printf("%d\n",k);
66     }
67 }        

POJ:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 using namespace std;
 6 #define maxn 501
 7 #define INF 0x3f3f3f3f
 8 int n,m,cost[maxn][maxn],dis[maxn],vis[maxn];
 9 int main()
10 {
11     while(~scanf("%d%d",&m,&n))
12     {
13         int u,v,c;
14         for(int i=1;i<=n;i++)
15         {
16             dis[i]=INF;vis[i]=0;
17             for(int j=1;j<=n;j++)
18                 cost[i][j]=i==j?0:INF;
19         }
20         for(int i=1;i<=m;i++)
21         {
22             scanf("%d",&u);
23             dis[u]=0;
24             vis[u]=1;
25         }
26         while(~scanf("%d%d%d",&u,&v,&c))
27             cost[u][v]=cost[v][u]=c;
28         for(int k=1;k<=n;k++)
29             for(int i=1;i<=n;i++)
30                 for(int j=1;j<=n;j++)
31                     cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
32         for(int i=1;i<=n;i++)
33             if(vis[i])
34                 for(int j=1;j<=n;j++)
35                     dis[j]=min(dis[j],cost[i][j]);
36         int ans=INF,pos;
37         for(int i=1;i<=n;i++)
38         {
39             int temp=-1;
40             for(int j=1;j<=n;j++)
41                 temp=max(temp,min(dis[j],cost[i][j]));
42             if(ans>temp)ans=temp,pos=i;
43         }
44         printf("%d\n",pos);
45     }
46     return 0;
47 }

View Code

转载于:https://www.cnblogs.com/wangmengmeng/p/5367819.html

Nyoj Fire Station相关推荐

  1. 《题目与解读》红书 训练笔记目录《ACM国际大学生程序设计竞赛题目与解读》

    虽然2012年出版的老书了,但是是由三次世界冠军的上海交大ACM队出版的书籍,选择的题目是ACM经典中的经典,书中有非常详细的题解,可以学到很多东西,值得一刷. 目录 第一部分 第一章 数学 1.1 ...

  2. 【HDOJ图论题集】【转】

    1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How ...

  3. 一系列图论问题[转]

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  4. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  5. DancingLinks刷题集

    HDU 3663 Power Stations 精确覆盖 题意:每个城市i有xi->yi天可以成为发射站,发射站覆盖范围为与该站有一条边链接的城市. 同时,每个每天城市必须且只能被一个发射站覆盖 ...

  6. php火车订票系统设计论文,基于JSP的火车票订票系统 JSP146(毕业设计+论文)

    摘  要 随着科学技术的不断提高,计算机科学日渐成熟,其强大的功能已为人们深刻认识,它已进入人类社会的各个领域并发挥着越来越重要的作用。 作为计算机应用的一部分使用计算机对火车信息进行管理,具有手工管 ...

  7. Python小白的数学建模课-07.选址问题

    选址问题是要选择设施位置使目标达到最优,是数模竞赛中的常见题型. 小白不一定要掌握所有的选址问题,但要能判断是哪一类问题,用哪个模型. 进一步学习 PuLP工具包中处理复杂问题的字典格式快捷建模方法. ...

  8. 【总结】Dancing Links

    1.精确覆盖. View Code 1 #include<cstdio> 2 #define INF 0x7FFFFFFF 3 #define MAXN 1000010 4 int n, ...

  9. π-Algorithmist分类题目(2)

    原题网站:Algorithmist,http://www.algorithmist.com/index.php/Main_Page π-Algorithmist分类题目(2) Set Theory U ...

  10. Third season twenty-third episode,Ross‘s thing on his butt???

    [Scene: Chandler and Joey's, Chandler and Joey are playing with the duck and the chick.] Joey: Hey, ...

最新文章

  1. es中的Plugin机制
  2. java多线程基础视频_【No996】2020年最新 Java多线程编程核心基础视频课程
  3. js怎么在一个div中嵌入另一网站_好程序员web前端学习路线分享HTML5常见面试题集锦一...
  4. java 参数返回_Java基础---Java中带参数返回值方法的使用(四十)
  5. java类与对象 演练 查找并修改姓名
  6. 软件开发架构的演变过程
  7. php时间戳对比,php+js+时间戳比较,输出不同内容
  8. typora代码块语言linux命令,typora工具的使用以及MarkDown语法
  9. 让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...
  10. REST笔记(三):一种标准的超媒体格式:Atom
  11. 代码设定的按钮与storyboard中的xib页面间的跳转
  12. ZPLII 指令参考
  13. html图片文字下方,css图片下边怎么加字
  14. SpringBoot整合Flowable工作流引擎框架
  15. 一个H.265/HEVC码流分析工具
  16. 深入浅出编译原理-3-词法分析器
  17. JS 判断手机当前是横屏还是竖屏
  18. 游戏风云----《劲舞团》,跑跑卡丁车,魔兽世界下载
  19. Java对接腾讯IM即时通话
  20. python现在流行的版本_何种版本的Python适合您

热门文章

  1. sufficient statistic
  2. Hadoop 集群 傻瓜式搭建手记 (一) 软件准备
  3. android 修改系统参数设置,Android获取与设置系统环境变量的方法指南
  4. 京东极速版上线,“杀”入三四五六。。。线市场,一起瞅瞅
  5. 使用EDD枚举域数据
  6. Flume 1.8.0 用户指南(Flume 1.8.0 User Guide)
  7. 面试必掌握之计算机网络
  8. 使用py 和flask 实现的服务器系统目录浏览,日志文件实时显示到网页的功能
  9. 20190613 一个SQL问题
  10. 【Ubuntu】ubuntu 16.04 设置root用户初始密码