zoukankan      html  css  js  c++  java
  • 特征距离

    题目链接: http://exercise.acmcoder.com/online/online_judge_ques?ques_id=3347&konwledgeId=40

    解题思路: 直接求出所有的最短路,以及最短路上的特征距离。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 typedef long long LL;
     5 const int inf=0x3f3f3f3f;
     6 const int MAXN=100005;
     7 const LL MON7 = 1e9+7;
     8 
     9 struct Edge
    10 {
    11     int v,c;
    12     Edge(){}
    13     Edge(int v, int c):v(v),c(c){}
    14 }edge[10*MAXN];
    15 int head[MAXN];
    16 int dis[MAXN];
    17 int fdis[MAXN];
    18 bool vis[MAXN];
    19 int next_edge[10*MAXN];
    20 int cnt;
    21 int n,m;
    22 void init()
    23 {
    24     memset(head,-1,sizeof(head));
    25     cnt=0;
    26 }
    27 void addEdge(int u,int v,int c)
    28 {
    29     edge[cnt]=Edge(v,c);
    30     next_edge[cnt]=head[u];
    31     head[u]=cnt++;
    32 }
    33 void spfa(int s,int e)
    34 {
    35     for (int i=0;i<=n;++i) dis[i]=inf;
    36     memset(fdis,0,sizeof(fdis));
    37     dis[s]=0;
    38     queue<int> q;
    39     q.push(s);
    40     vis[s]=1;
    41     while (!q.empty())
    42     {
    43         int u=q.front();
    44         q.pop();
    45         vis[u]=0;
    46         for (int i=head[u];i!=-1;i=next_edge[i])
    47         {
    48             int v=edge[i].v;
    49             int c=edge[i].c;
    50             if (dis[v]>dis[u]+c)
    51             {
    52                 dis[v]=dis[u]+c;
    53                 fdis[v]=max(fdis[u],c);
    54                 if (!vis[v]) q.push(v), vis[v]=1;
    55             } else if (dis[v]==dis[u]+c && max(fdis[u],c)>fdis[v])
    56             {
    57                 fdis[v]=max(fdis[u],c);
    58                 if (!vis[v]) q.push(v), vis[v]=1;
    59             }
    60         }
    61     }
    62     if (dis[e]==inf) printf("No answer
    ");
    63     else printf("%d
    ",fdis[e]);
    64 }
    65 
    66 int main()
    67 {
    68 #ifndef ONLINE_JUDGE
    69     freopen("test.txt","r",stdin);
    70 #endif // ONLINE_JUDGE
    71     int Case;
    72     int s,t;
    73     scanf("%d",&Case);
    74     for (int ct=1;ct<=Case;++ct)
    75     {
    76         scanf("%d%d",&n,&m);
    77         scanf("%d%d",&s,&t);
    78         init();
    79         int u,v,c;
    80         while(m--)
    81         {
    82             scanf("%d%d%d",&u,&v,&c);
    83             addEdge(u,v,c);
    84             addEdge(v,u,c);
    85         }
    86         printf("Case #%d: ", ct);
    87         spfa(s,t);
    88     }
    89     return 0;
    90 }
  • 相关阅读:
    POJ 1330 Nearest Common Ancestors(LCA Tarjan算法)
    LCA 最近公共祖先 (模板)
    线段树,最大值查询位置
    带权并查集
    转负二进制
    UVA 11437 Triangle Fun
    UVA 11488 Hyper Prefix Sets (字典树)
    UVALive 3295 Counting Triangles
    POJ 2752 Seek the Name, Seek the Fame (KMP)
    UVA 11584 Partitioning by Palindromes (字符串区间dp)
  • 原文地址:https://www.cnblogs.com/djingjing/p/8977914.html
Copyright © 2011-2022 走看看