HDU6005-Pandaland

本文介绍了一个寻找无向图中最小成本简单环的问题及其解决方案。通过枚举每条边并使用Dijkstra算法找到其余部分的最短路径来确定最小环的成本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Pandaland

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


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


1T50.
1m4000.
10000xi,yi10000.
1w105.
 

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
 

Recommend
jiangzijing2015
 


题意:给你一个m 个边的无向图,要求在图上找一个最小的环(边权)

解题思路:直接暴力枚举哪一个边。然后跑这两个点的最短路即可,Dijkstra时不跑这条边。加上这个边的权值即是这个最小环。(加个剪枝:当优先队列中的最小值大于等于ans 时直接不跑了)


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;

int s[8005],nt[8005],e[8005],w[8005];
int u[4005],v[4005],l[4005];
int m,x,y,xx,yy,ww;
map<pair<int,int>,int>mp;
int dis[8005],visit[8005],mi;

struct node
{
    int id,l;
    friend bool operator<(node a,node b)
    {
        return a.l>b.l;
    }
} pre,nt1;

void Dijkstra(int ss,int ee)
{
    memset(dis,INF,sizeof dis);
    memset(visit,0,sizeof visit);
    dis[ss]=0;
    pre.id=ss,pre.l=0;
    priority_queue<node>q;
    q.push(pre);
    while(!q.empty())
    {
        pre=q.top();
        q.pop();
        visit[pre.id]=1;
        if(pre.l>mi) break;
        for(int i=s[pre.id]; ~i; i=nt[i])
        {
            int v=e[i];
            if((pre.id==ss&&ee==v)||(pre.id==ee&&ss==v)||visit[v]) continue;
            if(dis[v]>dis[pre.id]+w[i])
            {
                dis[v]=dis[pre.id]+w[i];
                nt1.id=v;
                nt1.l=dis[v];
                q.push(nt1);
            }
        }
    }
}

int main()
{
    int t,cas=0;
    scanf("%d",&t);
    while(t--)
    {
        printf("Case #%d: ",++cas);
        scanf("%d",&m);
        memset(s,-1,sizeof s);
        int res=1,cnt=1;
        mp.clear();
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d%d%d%d",&x,&y,&xx,&yy,&ww);
            if(!mp[make_pair(x,y)]) mp[make_pair(x,y)]=res++;
            if(!mp[make_pair(xx,yy)]) mp[make_pair(xx,yy)]=res++;
            int a=mp[make_pair(x,y)],b=mp[make_pair(xx,yy)];
            u[i]=a,v[i]=b,l[i]=ww;
            nt[cnt]=s[a],s[a]=cnt,e[cnt]=b,w[cnt++]=ww;
            nt[cnt]=s[b],s[b]=cnt,e[cnt]=a,w[cnt++]=ww;
        }
        mi=INF;
        for(int i=1; i<=m; i++)
        {
            Dijkstra(u[i],v[i]);
            if(dis[v[i]]!=INF) mi=min(mi,dis[v[i]]+l[i]);
        }
        if(mi==INF) printf("0\n");
        else printf("%d\n",mi);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值