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. android DisplayMetrics 获取屏幕分辨率

    Android 提供DisplayMetircs 类可以很方便的获取分辨率.下面介绍 DisplayMetics 类: Andorid.util 包下的DisplayMetrics 类提供了一种关于显 ...

  2. javaWeb---文件上传(commons-FileUpload组件)

    FileUpload是Apache组织(www.apache.org)提供的免费的上传组件,但是FileUpload组件本身还依赖于commons组件,所以从Apache下载此组件的时候还需要连同co ...

  3. PostgreSQL中COUNT的各条件下(1亿条数据)例子

    test=# insert into tbl_time1 select generate_series(1,100000000),clock_timestamp(),now(); INSERT 0 1 ...

  4. python中多线程与非线程的执行性能对比

    此对比说明了一件事: 如果是IO型应用,多线程有优势, 如果是CPU计算型应用,多线程没必要,还有实现锁呢. #!/usr/bin/env python # -*- coding: utf-8 -*- ...

  5. 一致性hash算法简介与代码实现

    一.简介: 一致性hash算法提出了在动态变化的Cache环境中,判定哈希算法好坏的四个定义: 1.平衡性(Balance) 2.单调性(Monotonicity) 3.分散性(Spread) 4.负 ...

  6. C# DatrgridView表格控件的一些用法

    public class useDatrgrivView { string conn = null; string sqlComm = null; DataSet das = null; DataGr ...

  7. android 入门-动画与容器

    set 动画容器 可作为资源id添加R.anim.xxxx   可用于在样式表中添加  http://blog.csdn.net/liuhe688/article/details/6660823 in ...

  8. android 入门-Service实时向Activity通过BroadcastReceiver传递数据

    引文: http://www.cnblogs.com/linjiqin/p/3147764.html <RelativeLayout xmlns:android="http://sch ...

  9. Parallel.js初探

    今天闲着看了一下Parallel.js.这个库暂时貌似还没有什么中文的介绍(可能暂时用的人都不多吧).所以就只能上github找它得源码和介绍看看了.貌似它的代码也不多,以后可以深入研究一下. 先简单 ...

  10. C语言中结构体的位域(bit-fields)

    转自:http://blog.sina.com.cn/s/blog_6240b5980100tcba.html 有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一 ...