zoukankan      html  css  js  c++  java
  • HDU 6166 Senior Pan (最短路变形)

    题目链接

    Problem Description
    Senior Pan fails in his discrete math exam again. So he asks Master ZKC to give him graph theory problems everyday.
    The task is simple : ZKC will give Pan a directed graph every time, and selects some nodes from that graph, you can calculate the minimum distance of every pair of nodes chosen in these nodes and now ZKC only cares about the minimum among them. That is still too hard for poor Pan, so he asks you for help.
     
    Input
    The first line contains one integer T, represents the number of Test Cases.1≤T≤5.Then T Test Cases, for each Test Cases, the first line contains two integers n,m representing the number of nodes and the number of edges.1≤n,m≤100000
    Then m lines follow. Each line contains three integers xi,yi representing an edge, and vi representing its length.1≤xi,yi≤n,1≤vi≤100000
    Then one line contains one integer K, the number of nodes that Master Dong selects out.1≤K≤n
    The following line contains K unique integers ai, the nodes that Master Dong selects out.1≤ai≤n,ai!=aj
     
    Output
    For every Test Case, output one integer: the answer
     
    Sample Input
    1 5 6 1 2 1 2 3 3 3 1 3 2 5 1 2 4 2 4 3 1 3 1 3 5
     
    Sample Output
    Case #1: 2
     
    Source

    题意:

    给你一个有向图,然后给你k个点,求其中一个点到另一个点的距离的最小值

    题解:

    这个题很难想啊,不过是个超级棒的题目,首先将k个点划分为两个集合,可以通过二进制枚举来确保某个集合任意两点不在一个集合中,100000个点最多2^17次方,也就是划分17次

    每划分一次进行一次spfa,求其中最小值。

    代码:

    #include <stdio.h>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #include <deque>
    #include <iomanip>
    #include <iostream>
    #include <list>
    #include <map>
    #include <queue>
    #include <set>
    #include <utility>
    #include <vector>
    #define mem(arr, num) memset(arr, 0, sizeof(arr))
    #define _for(i, a, b) for (int i = a; i <= b; i++)
    #define __for(i, a, b) for (int i = a; i >= b; i--)
    #define IO                     
      ios::sync_with_stdio(false); 
      cin.tie(0);                  
      cout.tie(0);
    using namespace std;
    typedef long long ll;
    const ll inf = 0x3f3f3f3f;
    const double EPS = 1e-10;
    const ll mod = 1000000007LL;
    const int N = 100000 + 5;
    const int MAXN = 1 << 17;
    int dis[N], vis[N];
    int num, V, E;
    int po[N], belong[N];
    int minn = inf;
    struct edge{
      int point, value;
      edge(){}
      edge(int _p,int _v){point = _p, value = _v;}
    };
    vector <edge> e[N];
    void spfa() {
      _for(i, 1, V) dis[i] = inf;
      queue <int > q;
      _for(i, 1, num) {
        if(!belong[i]) {
          dis[po[i]] = 0;
          q.push(po[i]);
          vis[po[i]] = 1;
        }
      }
      while(!q.empty()) {
        int v = q.front();
        q.pop();
        vis[v] = 0;
        for(int i = 0; i < e[v].size(); i++){
          if(dis[e[v][i].point]>dis[v] + e[v][i].value) {
            dis[e[v][i].point] = dis[v] + e[v][i].value;
            if(!vis[e[v][i].point])
              vis[e[v][i].point] = 1,q.push(e[v][i].point);
          }
        }
      }
      _for(i, 1, num) if(belong[i]) minn = min(minn, dis[po[i]]);
    }
    int main() {
      int T;
      int s, ee, value;
      cin >> T;
      for(int k = 1; k <= T; k++) {
        cin >> V >> E;
        mem(po, 0);
        mem(belong, 0);
        minn = inf;
        _for(i, 1, V) e[i].clear();
        _for(i, 1, E){
          cin >> s >> ee >> value;
          e[s].push_back(edge(ee, value) );
        }
        cin >> num;
        _for(i, 1, num) cin >> po[i];
        sort(po+1, po + num +1);
        int len = log2(po[num]);
        _for(i, 0, len) {
          _for(j, 1, num) {
            if( po[j]&(1<<i) ) belong[j] = 1;
            else belong[j] = 0;
          }
          spfa();
        }
        printf("Case #%d: %d
    ",k,minn);
      }
      return 0;
    }
    宝剑锋从磨砺出 梅花香自苦寒来
  • 相关阅读:
    Raft协议备注
    领域建模笔记
    Spark编程模型
    Spark如何删除无效rdd checkpoint
    Parquet 列式存储格式
    SpringBoot中ConditionalOnClass注解的原理
    SpringBoot定制Sevlet容器原理
    分析JDK的动态代理原理
    简述SpringCloud底层原理
    简述SpringCloud框架
  • 原文地址:https://www.cnblogs.com/GHzcx/p/8876352.html
Copyright © 2011-2022 走看看