zoukankan      html  css  js  c++  java
  • HDU 6005 Pandaland (Dijkstra)

    题意:给定一个图,找出一个最小环。

    析:暴力枚举每一条,然后把边设置为最大值,以后就不用改回来了,然后跑一遍最短路,跑 n 次就好。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define sz size()
    #define pu push_up
    #define pd push_down
    //#define mp make_pair
    #define cl clear()
    //#define all 1,n,1
    #define FOR(x,n)  for(int i = (x); i < (n); ++i)
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e15;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 8000 + 50;
    const LL mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    int ans;
    
    struct Dijskstra{
      struct Edge{
        int from, to, dist;
        Edge() { }
        Edge(int f, int t, int v) : from(f), to(t), dist(v) { }
      };
    
      vector<Edge> edges;
      vector<int> G[maxn];
      bool done[maxn];
      int d[maxn];
    
      struct HeapNode{
        int d, u;
        HeapNode(){ }
        HeapNode(int dd, int uu) : d(dd), u(uu) { }
        bool operator < (const HeapNode &p) const{
          return d > p.d;
        }
      };
    
      void init(int n){
        for(int i = 0; i < n; ++i)  G[i].cl;
        edges.cl;
      }
    
      void addEdge(int from, int to, int dist){
        edges.pb(Edge(from, to, dist));
        G[from].pb(edges.sz-1);
      }
    
      int dijkstra(int s, int t){
        priority_queue<HeapNode>  pq;
        ms(d, INF);
        d[s] = 0;
        ms(done, false);
        pq.push(HeapNode(0, s));
    
        while(!pq.empty()){
          HeapNode x = pq.top();  pq.pop();
          int u = x.u;
          if(t == x.u)  return d[t];
          if(d[u] >= ans)  return ans;
          if(done[u])  continue;
          done[u] = true;
          for(int i = 0; i < G[u].sz; ++i){
            Edge &e = edges[G[u][i]];
            if(d[e.to] > d[u] + e.dist){
              d[e.to] = d[u] + e.dist;
              pq.push(HeapNode(d[e.to], e.to));
            }
          }
        }
        return d[t];
      }
    };
    
    map<P, int> mp;
    
    int cnt;
    int ID(const P &p){
      if(mp.count(p))  return mp[p];
      return mp[p] = cnt++;
    }
    Dijskstra dij;
    
    int main(){
      int T;  cin >> T;
      for(int kase = 1; kase <= T; ++kase){
        scanf("%d", &n);
        mp.cl;  cnt = 0;
        dij.init(n*2+10);
        for(int i = 0; i < n; ++i){
          int x1, y1, x2, y2, w;
          scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &w);
          int u = ID(P(x1, y1));
          int v = ID(P(x2, y2));
          dij.addEdge(u, v, w);
          dij.addEdge(v, u, w);
        }
        ans = INF;
        for(int i = 0; i < dij.edges.sz; i += 2){
          int val = dij.edges[i].dist;
          dij.edges[i].dist = dij.edges[i^1].dist = INF;
          ans = min(ans, dij.dijkstra(dij.edges[i].from, dij.edges[i].to) + val);
        }
        printf("Case #%d: %d
    ", kase, ans == INF ? 0 : ans);
      }
      return 0;
    }
    

      

  • 相关阅读:
    将博客搬至CSDN
    知道创建了SM30&nbsp;维护,怎么找…
    物料组件修改BAPI:BAPI_NETWORK_C…
    BAPI_NETWORK_COMP_REMOVE&nbsp;物料组…
    Python :Dive Into Python 中文版 读后感_第三章_内置数据结构
    Python :Dive Into Python 中文版 读后感_第三章_变量赋值
    Python :Dive Into Python 中文版 读后感_第四章__函数定义以及内置函数
    Python :Dive Into Python 中文版 读后感_第二章___需要注意的东西
    OutLook的网页版精美界面修改版
    .Net Framework2.0内部版本问题引起的错误!
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7445644.html
Copyright © 2011-2022 走看看