zoukankan      html  css  js  c++  java
  • loj 1002(spfa变形)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25828

    题意:求所有点到给定的目标顶点的路径上的权值的最大值的最小值。

    思路:spfa的应用,更新的时候判断max(dist[u],w(u,v))<dist[v]即可。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<queue>
     6 using namespace std;
     7 #define MAXN 555
     8 #define inf 1<<30
     9 #define FILL(a,b) memset(a,b,sizeof(a))
    10 
    11 struct Edge{
    12     int v,w;
    13     Edge(int _v,int _w):v(_v),w(_w){}
    14 };
    15 
    16 int n,m,dist[MAXN];
    17 bool mark[MAXN];
    18 vector<Edge>g[MAXN];
    19 
    20 void spfa(int vs)
    21 {
    22     FILL(mark,false);
    23     fill(dist,dist+n,inf);
    24     queue<int>que;
    25     que.push(vs);
    26     dist[vs]=0;
    27     while(!que.empty()){
    28         int u=que.front();
    29         que.pop();
    30         mark[u]=false;
    31         for(int i=0;i<g[u].size();i++){
    32             int v=g[u][i].v,w=g[u][i].w;
    33             if(max(dist[u],w)<dist[v]){
    34                 dist[v]=max(dist[u],w);
    35                 if(!mark[v]){
    36                     mark[v]=true;
    37                     que.push(v);
    38                 }
    39             }
    40         }
    41     }
    42 }
    43 
    44 int main()
    45 {
    46     int _case,u,v,w,vs,t=1;
    47     scanf("%d",&_case);
    48     while(_case--){
    49         scanf("%d%d",&n,&m);
    50         for(int i=0;i<n;i++)g[i].clear();
    51         while(m--){
    52             scanf("%d%d%d",&u,&v,&w);
    53             g[u].push_back(Edge(v,w));
    54             g[v].push_back(Edge(u,w));
    55         }
    56         scanf("%d",&vs);
    57         spfa(vs);
    58         printf("Case %d:
    ",t++);
    59         for(int i=0;i<n;i++){
    60             if(dist[i]==inf)puts("Impossible");
    61             else printf("%d
    ",dist[i]);
    62         }
    63     }
    64     return 0;
    65 }
    View Code
  • 相关阅读:
    hdu 4027 Can you answer these queries?
    Codeforces: Empty Triangle
    hdu 3006 The Number of set
    hdu 3645 Code Management System
    进度条作控件代码
    NORMAL
    callback
    三种形状匹配脚本
    移动点动画
    脚本管理
  • 原文地址:https://www.cnblogs.com/wally/p/3362671.html
Copyright © 2011-2022 走看看