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. wireshark_Couldn’t run /usr/sbin/dumpcap in child process: Permission denied

    关于Wireshark出现:Couldn't run /usr/sbin/dumpcap in child process: Permission denied Are you a member of ...

  2. php基础面试题1

    问题1:谈谈你对的PHP的基本认识. 回答:PHP是Hypertext Preprocessor(超文本预处理器)的简称,是一种用来开发动态网站的服务器端脚本语言. 问题2:什么是MVC? 回答:MV ...

  3. JavaScript基础——定义变量

    在JavaScript中使用变量来临时存储和访问来自JavaScript文件的数据.变量既可以指向简单的数据类型,如数字或者字符串:也可以指向更复杂的数据类型,比如对象. 在JavaScript中定义 ...

  4. 转:不再以讹传讹,GET和POST的真正区别

    如果有人问你,GET和POST,有什么区别?你会如何回答? 我的经历 前几天有人问我这个问题.我说GET是用于获取数据的,POST,一般用于将数据发给服务器之用. 这个答案好像并不是他想要的.于是他继 ...

  5. 重温WCF之会话Session(九)

    转载地址:http://blog.csdn.net/tcjiaan/article/details/8281782 每个客户端在服务器上都有其的独立数据存储区,互不相干,就好像A和服务器在单独谈话一样 ...

  6. python多线程之semaphore(信号量)

    #!/usr/bin/env python # -*- coding: utf-8 -*- import threading import time import random semaphore = ...

  7. [LeetCode] Merge Sorted Array

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

  8. linux中socket的理解

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

  9. 优秀的API接口设计原则及方法(转)

    一旦API发生变化,就可能对相关的调用者带来巨大的代价,用户需要排查所有调用的代码,需要调整所有与之相关的部分,这些工作对他们来说都是额外的.如果辛辛苦苦完成这些以后,还发现了相关的bug,那对用户的 ...

  10. JS 获取元素当前的样式信息

    HTMLElement.prototype.__defineGetter__("currentStyle", function () { return this.ownerDocu ...