HDU6005-Pandaland

来源:互联网 发布:淘宝分销货源平台 编辑:程序博客网 时间:2024/05/16 16:18

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
250 0 0 1 20 0 1 0 20 1 1 1 21 0 1 1 21 0 0 1 591 1 3 1 11 1 1 3 23 1 3 3 21 3 3 3 11 1 2 2 22 2 3 3 33 1 2 2 12 2 1 3 24 1 5 1 4
 

Sample Output
Case #1: 8Case #2: 4
 

Source
2016 CCPC-Final
 

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 longconst 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;}

原创粉丝点击