poj 2607 Fire Station (spfa)

Fire Station

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 3783   Accepted: 1337

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

题意:就是新建一个消防局使得居民点到消防局的最小值中最大的那个变得更小。若每个点都有消防局,输出1。

思路:用spfa求出原先的消防局到各个点的最短路,然后枚举每个居民点作为消防局,求得答案。

#include"stdio.h"
#include"string.h"
#include"vector"
#include"queue"
#include"iostream"
#include"algorithm"
using namespace std;
#define N 505
#define max(a,b) ((a)>(b)?(a):(b))
const int inf=0x7fffffff;
int a[N],head[N],newd[N],dis[N],q[N];
int t;
struct node
{
    int u,v,w,next;
}e[N*20];
void add(int u,int v,int w)
{
    e[t].u=u;
    e[t].v=v;
    e[t].w=w;
    e[t].next=head[u];
    head[u]=t++;
}
void spfa(int s)
{
    int i,l,r,u,v;
    bool in[N];
    memset(in,0,sizeof(in));
    l=r=0;
    q[r++]=s;
    newd[s]=0;
    while(l<r)          //循环队列
    {
        u=q[l++%N];
        in[u]=0;
        for(i=head[u];i!=-1;i=e[i].next)
        {
            v=e[i].v;
            if(newd[u]<inf&&newd[v]>newd[u]+e[i].w)
            {
                newd[v]=newd[u]+e[i].w;
                if(!in[v])
                {
                    in[v]=1;
                    q[r++%N]=v;
                }
            }
        }
    }
}
int main()
{
    int i,j,n,f,u,v,w;
    char str[100];
    while(scanf("%d%d",&f,&n)!=-1)
    {
        memset(a,0,sizeof(a));
        memset(head,-1,sizeof(head));
        t=0;
        for(i=0;i<f;i++)
        {
            scanf("%d",&j);
            a[j]=1;
        }
        getchar();
        while(fgets(str,100,stdin)&&strlen(str)-1)     //注意输入
        {
            str[strlen(str)-1]!='\0';
            sscanf(str,"%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        if(f==n)
        {
            printf("1\n");
            continue;
        }
        for(i=1;i<=n;i++)
        {
            if(a[i])
                newd[i]=0;
            else
                newd[i]=inf;
        }
        for(i=1;i<=n;i++)
            if(a[i])
                spfa(i);
        int mmax=-1;
        for(i=1;i<=n;i++)
        {
            mmax=max(mmax,newd[i]);
            dis[i]=newd[i];     //dis保存原先值
        }
        for(i=1;i<=n;i++)
        {
            if(!a[i])
            {
                int tmp=0;
                spfa(i);
                for(j=1;j<=n;j++)
                {
                    tmp=max(tmp,newd[j]);
                }
                if(tmp<mmax)
                {
                    mmax=tmp;
                    f=i;
                }
            }
            memcpy(newd,dis,sizeof(dis));    //恢复原先值
        }
        printf("%d\n",f);
    }
    return 0;
}
时间: 2024-10-08 02:20:24

poj 2607 Fire Station (spfa)的相关文章

poj 1860 Currency Exchange(SPFA)

题目链接:http://poj.org/problem?id=1860 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can b

POJ 2607 Fire Station

枚举+最短路问题. 题意依然晦涩难懂. 新建一个消防站n 可以使得所有交叉路口到最近的一个消防站的距离中最大值减小,且n 是满足条件的交叉路口序号中序号最小的. 先每个消防站做SPFA.找到所有点 到最近消防站的 距离. 然后枚举 每个不是消防站的点,找到距离这个点的最大距离.然后比对 最大是否更新了. ORZ的是,输入边的时候要EOF.简直-- 谁是出题人,看我不把他脸按到键OWIHW#ROIJHA(P*#RY(#*Y(*#@UISHIUOHEOIF #include<cstdio> #in

poj 1860 Currency Exchange (SPFA、正权回路 bellman-ford)

链接:poj 1860 题意:给定n中货币,以及它们之间的税率,A货币转化为B货币的公式为 B=(V-Cab)*Rab,其中V为A的货币量, 求货币S通过若干此转换,再转换为原本的货币时是否会增加 分析:这个题就是判断是否存在正权回路,可以用bellman-ford算法,不过松弛条件相反 也可以用SPFA算法,判断经过转换后,转换为原本货币的值是否比原值大... bellman-ford    0MS #include<stdio.h> #include<string.h> str

poj 1459 Power Network (dinic)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23059   Accepted: 12072 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

POJ 3669 Meteor Shower(流星雨)

POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS    Memory Limit: 65536K Description 题目描述 Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her sa

HDU 2680Choose the best route (SPFA)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 百度之星编程大赛--您报名了吗? 杭电ACM 2014暑期集训队--选拔安排~ Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis

HDU 1045 - Fire Net (最大独立集)

题意:给你一个正方形棋盘.每个棋子可以直线攻击,除非隔着石头.现在要求所有棋子都不互相攻击,问最多可以放多少个棋子. 这个题可以用搜索来做.每个棋子考虑放与不放两种情况,然后再判断是否能互相攻击来剪枝.最后取可以放置的最大值. 这里我转化成求最大独立集来做. 首先将每个空地编号,对于每个空地,与该位置可以攻击到的空地连边.找最多的空地使得不互相攻击,即求该图的最大独立集.与搜索做法基本一致,但是说法略有不同. 1 #include<iostream> 2 #include<cstring

POJ 3253 Fence Repair (优先队列)

POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN (1 ≤ N ≤ 20,000) planks of wood, each having some integer lengthLi (1 ≤ Li ≤ 50,000) units. He the

POJ 3292 Semi-prime H-numbers(数)

Semi-prime H-numbers Description This problem is based on an exercise of David Hilbert, who pedagogically suggested that one study the theory of 4n+1 numbers. Here, we do only a bit of that. An H-number is a positive number which is one more than a m