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. Linux mpstat字段解析

    mpstat是Multiprocessor Statistics的缩写,是实时系统监控工具.其报告与CPU的一些统计信息,这些信息存放在/proc/stat文件中.在多CPUs系统里,其不但能查看所有 ...

  2. Redis内存管理(一)

    Redis数据库的内存管理函数有关的文件为:zmalloc.h和zmalloc.c. Redis作者在编写内存管理模块时考虑到了查看系统内是否安装了TCMalloc或者Jemalloc模块,这两个是已 ...

  3. 解决eclipseMavne的web项目debug时没有源码

  4. Linux下C语言多线程,网络通信简单聊天程序

    http://www.cnblogs.com/zhuxianji/archive/2011/01/06/1928970.html

  5. .NET生成带Logo的二维码

    使用ThoughtWorks.QRCode生成,利用这个库来生成带Logo的二维码(就是中间嵌了一个图片的二维码),直接见代码: HttpContext context = HttpContext.C ...

  6. POJ1699 HDU 1560 Best Sequence(AC自动机 最短路)

    曾写过迭代加深搜索的方法,现在使用在AC自动上跑最短路的方法 dp[i][j]表示状态为到节点i,模式串是否包含的状态为j的最短串的长度,则状态转移方程为: dp[nx][ny] = min(dp[x ...

  7. NuGet安装和使用

    1. NuGet是什么? NuGet is a Visual Studio 2010 extension that makes it easy to add, remove, and update l ...

  8. phpMailer邮件发送

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. hdu 5437 优先队列+模拟 **

    比赛的时候虽然考虑到没门的情况,但是写了几组都能过,就没想了,23333,差一行代码就能A,遗憾~~ #include<cstdio> #include<iostream> # ...

  10. linux中预留的$变量

    $0表示bash脚本的文件名 $1表示第一个参数 $*表示参数列表$0, $1, $2… $@表示"$1"/"$2"...每个变量都是独立的,用双引号括起来 $ ...