zoukankan      html  css  js  c++  java
  • poj1797

    题目大意:

    给你以T, 代表T组测试数据,一个n代表有n个点, 一个m代表有m条边, 每条边有三个参数,a,b,c表示从a到b的这条路上最大的承受重量是c,

    让你找出一条线路,要求出在这条线路上的最小承重, 在所有其他线路最大。

    题目分析:

    这里只要将spfa进行一下变形就可以解决这问题了。

    首先 我们的dist数组,起点位置要初始化为 INF, 其他位置初始化为 0

    然后我们更新 dist 数组, 结果输出 dist[n]就行了

    为什么这样写: 因为我们每次要找 所有路径中的最大边的最小一个, 说的可能有写绕口

    递推式是: dist[e] = max(dist[e], min(dist[s], G[s][i]) );

    下面是代码:

     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <queue>
     7 using namespace std;
     8 #define INF 0xfffffff
     9 #define maxn 1050
    10 
    11 struct Edge
    12 {
    13     int e;
    14     long long w;
    15 };
    16 vector<Edge> G[maxn];
    17 long long dist[maxn];
    18 bool vis[maxn];
    19 int m, n;
    20 long long Spfa()
    21 {
    22     Edge P, Pn;
    23     P.e = 1, P.w = 0;
    24     queue <Edge> Q;
    25     Q.push(P);
    26 
    27     while( !Q.empty() )
    28     {
    29         P = Q.front();
    30         Q.pop();
    31         vis[P.e] = false;
    32         int len = G[P.e].size();
    33 
    34         for(int i=0; i<len; i++)
    35         {
    36             Pn = G[P.e][i];
    37 
    38             if(dist[Pn.e] < min(dist[P.e],Pn.w) )
    39             {
    40                 dist[Pn.e] = min(dist[P.e],Pn.w);
    41 
    42                 if(!vis[Pn.e])
    43                 {
    44                     vis[Pn.e] = true;
    45                     Q.push(Pn);
    46                 }
    47             }
    48         }
    49     }
    50     return dist[n];
    51 }
    52 void Init()
    53 {
    54     for(int i=1; i<=n ;i++)
    55     {
    56         G[i].clear();
    57         vis[i] = false;
    58         dist[i] = 0;
    59     }
    60     dist[1] = INF;
    61 }
    62 int main()
    63 {
    64     int T, cas = 1;
    65     Edge P;
    66     cin >> T;
    67 
    68     while(T--)
    69     {
    70         scanf("%d%d",&n,&m);
    71 
    72         Init();
    73         for(int i=0; i<m; i++)
    74         {
    75             int a, b, c;
    76             scanf("%d%d%d",&a,&b,&c);
    77             P.e = b, P.w = c;
    78 
    79             G[a].push_back(P);
    80 
    81             P.e = a;
    82 
    83             G[b].push_back(P);
    84         }
    85         long long ans = Spfa();
    86 
    87         printf("Scenario #%d:
    %lld
    ",cas++,ans);
    88         if(T)
    89             printf("
    ");
    90 
    91     }
    92     return 0;
    93 }
  • 相关阅读:
    MFC tab页面中获到其它页面的数据
    sqlite数据库中"Select * From XXX能查到数据,但是Select DISTINCT group From xxx Order By group却查不出来
    关闭程序出现崩溃(exe 已触发了一个断点及未加载ucrtbased.pdb)
    springboot 通用Mapper使用
    springBoot 发布war包
    springCloud Zuul网关
    springboot hystrix turbine 聚合监控
    springBoot Feign Hystrix Dashboard
    springBoot Ribbon Hystrix Dashboard
    springBoot Feign Hystrix
  • 原文地址:https://www.cnblogs.com/chenchengxun/p/4152672.html
Copyright © 2011-2022 走看看