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

    Problem Description


    Mr. Panda lives in Pandaland. There are many cities in Pandaland. Each city can be treated as a point on a 2D plane. Different cities are located in different locations.
    There are also M bidirectional roads connecting those cities. There is no intersection between two distinct roads except their endpoints. Besides, each road has a cost w.
    One day, Mr. Panda wants to find a simple cycle with minmal cost in the Pandaland. To clarify, a simple cycle is a path which starts and ends on the same city and visits each road at most once.
    The cost of a cycle is the sum of the costs of all the roads it contains.

    Input


    The first line of the input gives the number of test cases, T. T test cases follow.
    Each test case begins with an integer M.
    Following M lines discribes roads in Pandaland.
    Each line has 5 integers x1,y1,x2,y2, w, representing there is a road with cost w connecting the cities on (x1,y1) and (x2,y2).

    Output


    For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the cost Mr. Panda wants to know.
    If there is no cycles in the map, y is 0.
    limits
    ∙1≤T≤50.
    ∙1≤m≤4000.
    ∙−10000≤xi,yi≤10000.
    ∙1≤w≤105.

    Sample Input

    2
    5
    0 0 0 1 2
    0 0 1 0 2
    0 1 1 1 2
    1 0 1 1 2
    1 0 0 1 5
    9
    1 1 3 1 1
    1 1 1 3 2
    3 1 3 3 2
    1 3 3 3 1
    1 1 2 2 2
    2 2 3 3 3
    3 1 2 2 1
    2 2 1 3 2
    4 1 5 1 4
    

    Sample Output

    Case #1: 8
    Case #2: 4
    

    Source


    2016 CCPC-Final

    题解


    题意:给出一个无向图,问其中的最小简单环(经过每条边仅一次),若找不到环,输出0。
    枚举每一条边,删除它,再求这两点之间的最短路即可。
    当然,直接这么做,时间(O(m(n+m)logn)),因此需要剪枝:
    若当前最小环值为ans,那么在最短路过程中,当前最小边的长度大于ans,直接退出即可。

    参考代码

    #include <map>
    #include <queue>
    #include <cmath>
    #include <cstdio>
    #include <complex>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    #define ll long long
    #define inf 5000000000LL
    #define PI acos(-1)
    #define REP(i,x,n) for(int i=x;i<=n;i++)
    #define DEP(i,n,x) for(int i=n;i>=x;i--)
    #define mem(a,x) memset(a,x,sizeof(a))
    using namespace std;
    ll read(){
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    void Out(ll a){
        if(a<0) putchar('-'),a=-a;
        if(a>=10) Out(a/10);
        putchar(a%10+'0');
    }
    const int N=80005;
    map<int,map<int,int> >pos,vis;
    int sz;
    int find(int x,int y){
        if(!pos[x][y]) pos[x][y]=++sz;
        return pos[x][y];
    };
    struct node{
        int to,nxt;
        ll cost;
        node(){}
        node(int s1,int s2,ll s3){
            to=s1;nxt=s2;cost=s3;
        }
        bool operator < (const node &an) const{
            return cost>an.cost;
        }
    }Path[N];
    int head[N],e;
    void Addedge(int u,int v,int w){
        Path[++e]=node(v,head[u],(ll)w);
        head[u]=e;
    }
    void Init(){
        sz=0;e=0;
        mem(head,0);
        pos.clear();
        vis.clear();
    }
    ll dis[N],ans;
    bool book[N];
    ll Dijkstra(int s,int t,ll w){
        priority_queue<node>que;
        REP(i,0,sz) dis[i]=inf;
        mem(book,false);
        dis[s]=0;
        que.push(node(s,-1,0));
        int u,v;
        struct node cur;
        while(!que.empty()){
            cur=que.top();
            que.pop();u=cur.to;
            if(cur.cost>=w) break;;
            if(book[u]) continue;
            book[u]=true;
            for(int i=head[u];i;i=Path[i].nxt){
                  v=Path[i].to;
                  if(dis[v]>dis[u]+Path[i].cost){
                        dis[v]=dis[u]+Path[i].cost;
                        que.push(node(v,-1,dis[v]));
                  }
            }
        }
        return dis[t];
    }
    int main(){
        int T=read();
        REP(i,1,T){
           Init();
           int m=read();
           int u,v,w,x1,y1,x2,y2;
           REP(i,1,m){
               x1=read();y1=read();
               x2=read();y2=read();
               w=read();
               u=find(x1,y1);v=find(x2,y2);
               Addedge(u,v,w);
               Addedge(v,u,w);
           }
           ans=inf;ll tmp;
           REP(i,1,sz){
               for(int k=head[i];k;k=Path[k].nxt){
                    u=i;v=Path[k].to;
                    if(vis[u][v]||vis[v][u]) continue;
                    vis[u][v]=vis[v][u]=1;
                    tmp=Path[k].cost;
                    Path[k].cost=inf;
                    ans=min(ans,Dijkstra(u,v,ans-tmp)+tmp);
                    Path[k].cost=tmp;
               }
           }
           printf("Case #%d: %lld
    ",i,ans==inf?0:ans);
        }
        return 0;
    }
    
  • 相关阅读:
    [学习笔记] 网络最大流的HLPP算法
    [学习笔记] LCT 初步
    js中函数的原型及继承
    关于js中函数的一点总结
    关于css实现水平及垂直居中的方法记录
    js基础总结03 --操作数组
    近期学习es6后对变量提升及let和const的一点思考
    用css和js实现侧边菜单栏点击和鼠标滑动特效
    用css或js实现文本输入框的特效
    Jmeter怎样打印日志
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/7218707.html
Copyright © 2011-2022 走看看