poj 2607 Fire Station

来源:互联网 发布:淘宝运营计划表 编辑:程序博客网 时间:2024/05/01 12:03

枚举点求单源最短路

Memory: 748K Time: 204MSLanguage: G++ Result: Accepted

 

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<queue>using namespace std;const int maxn=505;const int INF=0x3fffffff;struct node{    int v, w, next;}edge[maxn*40];int n, m, pre[maxn], inqueue[maxn], cnt, fire[maxn], d[maxn], dmin[maxn], vis[maxn];void add_edge(int s, int e, int w){    edge[cnt].v=e;    edge[cnt].w=w;    edge[cnt].next=pre[s];    pre[s]=cnt++;}int max(int x, int y){return x>y?x:y;}int min(int x, int y){return x<y?x:y;}void init(){    int i, x, y, w;    cnt=0;    memset(vis, 0, sizeof(vis));    memset(pre, -1, sizeof(pre));    for(i=0; i<m; i++){        scanf("%d", &fire[i]);        vis[fire[i]]=1;    }    while(scanf("%d%d%d", &x, &y, &w)!=EOF){        add_edge(x, y, w);        add_edge(y, x, w);    }}void spfa(int st){    queue<int > q;    memset(inqueue, 0, sizeof(inqueue));    for(int i=1; i<=n; i++)    d[i]=INF;    d[st]=0;    q.push(st);    inqueue[st]=1;    int head, s;    while(!q.empty()){        head=q.front();        inqueue[head]=0;        q.pop();        for(s=pre[head];s!=-1;s=edge[s].next){            if(d[edge[s].v]>d[head]+edge[s].w){                d[edge[s].v]=d[head]+edge[s].w;                inqueue[edge[s].v]=1;                q.push(edge[s].v);            }        }    }}void solve(){    int i, j, ans=0, loc=1, tmp;    for(i=1; i<=n; i++)dmin[i]=INF;    for(i=0; i<m; i++){        spfa(fire[i]);        for(j=1; j<=n; j++)        if(dmin[j]>d[j])        dmin[j]=d[j];    }    for(i=1; i<=n; i++)if(dmin[i]>ans)ans=dmin[i];    for(i=1; i<=n; i++){        if(!vis[i]){            tmp=0;            spfa(i);            for(j=1; j<=n; j++)                tmp=max(tmp, min(dmin[j], d[j]));            if(tmp<ans){                ans=tmp;                loc=i;            }        }    }    printf("%d\n", loc);}int main(){    //freopen("1.txt", "r", stdin);    scanf("%d%d", &m, &n);    init();    solve();    return 0;}


 

原创粉丝点击