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. 解决Fragment在Viepager中切换不保存状态的问题

    在FragmentPagerAdapter中重写以下方法: @Override public Object instantiateItem(ViewGroup container, int posit ...

  2. XPath的基本使用

    XPath XPath 使用路径表达式来选取 XML 文档中的节点或节点集. 路径表达式 结果 bookstore 选取 bookstore 元素的所有子节点. /bookstore 选取根元素 bo ...

  3. 瞧一瞧迷一般的SQLDA

    With static SQL, host variables used in embedded SQL statements are known at application compile tim ...

  4. NYOJ题目62笨小熊

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAr4AAAK1CAIAAAChInrhAAAgAElEQVR4nO3dO3LjutaG4X8Szj0Qxx

  5. openfire 服务器名称:后面的黄色叹号

    然后点击重新获取证书,然后重新启动服务,问题解决!

  6. GPS NEMA 0183协议

    转自:http://www.cnblogs.com/xidongs/archive/2011/02/01/1948689.html 一. NMEA0183标准语句(GPS常用语句)$GPGGA例:$G ...

  7. 谈谈Delphi中的类和对象4---类是一种对数据和操作高度的封装机制 && 类是一种代码重用机制

    五.类是一种对数据和操作高度的封装机制 1)数据封装 unit Unit2; interface type TEmployee = class; private FName: String; publ ...

  8. HDU1294 Rooted Trees Problem(整数划分 组合数学 DP)

    讲解见http://www.cnblogs.com/IMGavin/p/5621370.html, 4 可重组合 dfs枚举子树的节点个数,相乘再累加  1 #include<iostream& ...

  9. maven pom.xml 说明

    本文复制于:http://blog.csdn.net/zhuxinhua/article/details/5788546 一.pom.xml示例 <project> <modelVe ...

  10. linux中socket的理解

    对linux中socket的理解 一.socket 一般来说socket有一个别名也叫做套接字. socket起源于Unix,都可以用“打开open –> 读写write/read –> ...