C - Fire Station

Description

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. 

Input

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.

Output

You are to output a single integer: 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.

Sample Input

1 6
2
1 2 10
2 3 10
3 4 10
4 5 10
5 6 10
6 1 10

Sample Output

5
这是仿照人家写的SPFA,然后补上其他3个最短路算法
 /好长时间没写过最短路了,好多基础的东西都不记得了  c
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue> using namespace std; const int N = , INF = ; int head[N], nc, n; ///
int dis[N]; ///计算距离的数组
int stk[N], f, r; ///用数组模拟栈,
bool vis[N]; ///在用spfa时的标记数组 struct Edge{ ///边
int to, next, cost;
}edge[]; void add(int a, int b, int c) ///加边函数,双向
{
edge[nc].to = b;
edge[nc].next = head[a];
edge[nc].cost = c;
head[a] = nc++; edge[nc].to = a;
edge[nc].next = head[b];
edge[nc].cost = c;
head[b] = nc++;
} void fire_spfa(int fire[], int num)
{
memset(vis, false, sizeof(vis)); f = r = ; ///初始化栈 for(int i = ; i < num; i++) ///初始化每个消防站到其他点的距离
{
int t = fire[i];
if(!vis[t])
{
vis[t] = true;
dis[stk[r++] = t] = ;///初始化,并将当前点入栈
}
} while(f != r) ///如果栈不为空
{
int now = stk[f++]; ///从栈中取出栈顶元素
if(f == N) f = ; ///防止栈空间不够 vis[now] = false; ///将当前结点标记
for(int i = head[now]; i != -; i = edge[i].next) ///从边表中取出元素
{
int to = edge[i].to;
int cot = edge[i].cost; if(dis[to] > dis[now] + cot) ///进行松弛操作
{
dis[to] = dis[now] + cot;
if(!vis[to]) ///如果前驱没有访问过,
{
stk[r++] = to; ///入栈
if(r == N)
r = ;
vis[to] = true; ///标记
}
}
}
}
} int spfa(int src)
{
memset(vis, false, sizeof(vis));
f = ;
r = ; vis[src] = true;
int d[N];
for(int i = ; i <= n; ++i) ///从当前结点到其余所有结点距离初始化
{
d[i] = INF;
} d[src] = ;
stk[] = src; while(f != r)
{
int now = stk[f++];
if(f == N)
f = ;
vis[now] = false; for(int i = head[now]; i != -; i = edge[i].next)
{
int to = edge[i]. to;
int cot = edge[i].cost; if(d[to] > d[now] + cot)
{
d[to] = d[now] + cot;
if(!vis[to])
{
stk[r++] = to;
vis[to] = true;
if(r == N)
r = ;
}
}
}
} int ans = ;
for(int i = ; i <= n; ++i)
{
ans = max(ans, min(d[i], dis[i]));
}
return ans;
} int main()
{
int ff, fire[N], a, b, c; memset(head, -, sizeof(head));
nc = ;
scanf("%d%d", &ff, &n);
for(int i = ; i < ff; ++i)
{
scanf("%d", fire+i);
} while(scanf("%d%d%d", &a, &b, &c) != EOF)
{
add(a, b, c);
} for(int i = ; i <= n; ++i)
dis[i] = INF; fire_spfa(fire, ff); int id = , ans = INF; for(int i = ; i <= n; ++i)
{
if(dis[i] != )
{
int tp = spfa(i);
if(ans > tp)
{
ans = tp;
id = i;
}
}
} printf("%d\n", id);
return ;
}

NUC_HomeWork1 -- POJ2067(最短路)的更多相关文章

  1. bzoj1001--最大流转最短路

    http://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路:这应该算是经典的最大流求最小割吧.不过题目中n,m<=1000,用最大流会TLE, ...

  2. 【USACO 3.2】Sweet Butter(最短路)

    题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...

  3. Sicily 1031: Campus (最短路)

    这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...

  4. 最短路(Floyd)

    关于最短的先记下了 Floyd算法: 1.比较精简准确的关于Floyd思想的表达:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B.所以,我们假设maz ...

  5. bzoj1266最短路+最小割

    本来写了spfa wa了 看到网上有人写Floyd过了 表示不开心 ̄へ ̄ 改成Floyd试试... 还是wa ヾ(。`Д´。)原来是建图错了(样例怎么过的) 结果T了 于是把Floyd改回spfa 还 ...

  6. HDU2433 BFS最短路

    Travel Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  7. 最短路(代码来源于kuangbin和百度)

    最短路 最短路有多种算法,常见的有一下几种:Dijstra.Floyd.Bellman-Ford,其中Dijstra和Bellman-Ford还有优化:Dijstra可以用优先队列(或者堆)优化,Be ...

  8. Javascript优化细节:短路表达式

    什么是短路表达式? 短路表达式:作为"&&"和"||"操作符的操作数表达式,这些表达式在进行求值时,只要最终的结果已经可以确定是真或假,求值过程 ...

  9. Python中三目计算符的正确用法及短路逻辑

    今天在看别人代码时看到这样一种写法, 感觉是个挺容易踩到的坑, 搞清楚后写出来备忘. 短路逻辑 Python中进行逻辑运算的时候, 默认采用的是一种叫做短路逻辑的运算规则. 名字是很形象的, 下面直接 ...

随机推荐

  1. 关于 UICollectionViewCell 的一些陷阱

    如果直接使用 UICollectionViewCell 的自带属性 selected 来自定义一些样式,如: - (void)setSelected:(BOOL)selected { [super s ...

  2. Mysql之高可用

    使用缓存Memcache, 1,可使用Hash算法由客户端决定路由到哪个Memcache服务器上:客户端完全不用关心数据存储在哪个Memcache服务器上:完全隔离了客户端与服务端:由于是Hash,在 ...

  3. Union函数

    . 共用体声明和共用体变量定义 共用体(参考“共用体”百科词条)是一种特殊形式的变量,使用关键字union来定义 共用体(有些人也叫"联合")声明和共用体变量定义与结构体十分相似. ...

  4. linux vi 中按了ctrl+s后没法退出

    linux vi 中按了ctrl+s后无法退出 Linux 中使用vi编辑文件 不小心按了Ctrl + S (习惯了) 结果终端就跟死了一样, 解决办法: Ctrl+Q

  5. 数据结构之DFS与BFS实现

    本文主要包括以下内容 邻接矩阵实现无向图的BFS与DFS 邻接表实现无向图的BFS与DFS 理论介绍 深度优先搜索介绍 图的深度优先搜索(Depth First Search),和树的先序遍历比较类似 ...

  6. linux 普通用户切换成root免密码

    [root@ok ~]# vim /etc/pam.d/su 下面是/etc/pam.d/su文件的内容 #%PAM-1.0 auth sufficient pam_rootok.so # Uncom ...

  7. 【jquery】 【jQuery技术内幕】阅读笔记 一

    jQuery( object ) jquery在构造对象时,除了可以用十分好用的css选择器来查找DOM,还可以传入一个javascript对象来生成一个jquery对象. // JS var foo ...

  8. 设计模式学习之策略模式(Strategy,行为型模式)(13)

    转载地址:http://www.cnblogs.com/zhili/p/StragetyPattern.html 一.引言 本文要介绍的策略模式也就是对策略进行抽象,策略的意思就是方法,所以也就是对方 ...

  9. android 入门-android属性介绍

      android:visibility="gone" 不保留view控件所占有的空间 隐藏 android:visibility="invisible" 保留 ...

  10. android 入门-生命周期 activity

    生命周期 activity http://blog.csdn.net/android_tutor/article/details/5772285 http://www.cnblogs.com/John ...