hdu6005 Pandaland(求无向图最小环—dij算法暴力+剪枝)

版权声明:本文为博主原创文章,转载请说明出处。 https://blog.csdn.net/xianpingping/article/details/83509565

Pandaland

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1434    Accepted Submission(s): 365


 

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

Recommend

jiangzijing2015   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 

这道题就是给了一个 图,然后求最小环。

思路:

由于边比较少,一共4000条无向边,所以可以枚举每一条边当作环的两个端点,跑一次dij的复杂度大概是:O((n+m)logn),那么m条边,大概是:O(m*(n+m)logn).这样就要剪枝。我们的剪枝方法是,在dij算法中,如果选择出来的边,比我们当前求得的最小环的长度还要大的话,那么这条边肯定就不取。

这样就能过了。

坑点:

这里我的mp用来记录点。然后用int【100000】【100000】mle了,改成了map<int,<int,int>>mp。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=80005;
const int maxm=80005;
const long long INF=1e18;
int cnt;
int n;
int num;

map<int,map<int,int> >mp;

struct qnode{
    int u;
    int to,next;
    LL w;
    qnode(){}
    qnode(int s0,int s1,int s2,LL s3){
        u=s0,to=s1;next=s2;w=s3;
    }
    bool operator < (const qnode &an) const{
        return w>an.w;
    }
}G[maxn];

int head[maxm];
bool vis[maxn];
LL dist[maxn];
LL ans;
void add(int u,int v,LL w){
    G[++cnt]=qnode(u,v,head[u],w);
    head[u]=cnt;
}
void init(){
    memset(head,-1,sizeof(head));
    cnt=0;
}

void dij(int start,int endd){
    priority_queue<qnode>que;
    for(int i=1;i<=num;i++){
        dist[i]=INF;
    }
    memset(vis,0,sizeof(vis));
    dist[start]=0;
    que.push(qnode(start,start,-1,0));
    qnode tmp;
    int u,v;
    while(!que.empty()){
        tmp=que.top();
        que.pop();
        u=tmp.to;
        if(tmp.w>=ans)///就是在这里优化剪枝的
            break;
        if(vis[u])
            continue;
        vis[u]=true;
        for(int i=head[u];i;i=G[i].next){
            v=G[i].to;
            LL cost=G[i].w;///记住一定要用LL
            if(!vis[v] && dist[v]>dist[u]+cost){
                dist[v]=dist[u]+cost;
                que.push(qnode(u,v,-1,dist[v]));
            }
        }
    }
}

int findd(int x,int y){
    if(!mp[x][y]){
        mp[x][y]=++num;
    }
    return mp[x][y];
}
LL minn(LL a,LL b){
    if(a>b)
        return b;
    else
        return a;
}
int main()
{
    int t,x;
    int a,b,c,d;
    int cas=0;
    scanf("%d",&t);
    while(t--){
            cas++;
        scanf("%d",&n);
        init();
        num=0;
       mp.clear();
        memset(G,0,sizeof(G));
        for(int i=1;i<=n;i++){
           cin>>a>>b>>c>>d>>x;
           if(x)
           {
               int pp1=findd(a,b);
               int pp2=findd(c,d);
               add(pp1,pp2,x);
               add(pp2,pp1,x);
           }
        }
        ans=INF;
        for(int i=1;i<=cnt;i++){
            int u=G[i].u;
            int v=G[i].to;
            int w=G[i].w;
            G[i].w=INF;
            dij(u,v);
            LL lala=dist[v];
            G[i].w=w;
            ans=minn(lala+w,ans);
        }
        if(ans==INF)
            ans=0;
        cout<<"Case #"<<cas<<": "<<ans<<endl;

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xianpingping/article/details/83509565