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\n",i,ans==inf?0:ans);
}
return 0;
}

【HDU 6005】Pandaland(Dijkstra)的更多相关文章

  1. 【HDU - 3085】Nightmare Ⅱ(bfs)

    -->Nightmare Ⅱ 原题太复杂,直接简单的讲中文吧 Descriptions: X表示墙 .表示路 M,G表示两个人 Z表示鬼 M要去找G但是有两个鬼(Z)会阻碍他们,每一轮都是M和G ...

  2. 【HDU 2853】Assignment (KM)

    Assignment Problem Description Last year a terrible earthquake attacked Sichuan province. About 300, ...

  3. 【HDU - 4345 】Permutation(DP)

    BUPT2017 wintertraining(15) #8F 题意 1到n的排列,经过几次置换(也是一个排列)回到原来的排列,就是循环了. 现在给n(<=1000),求循环周期的所有可能数. ...

  4. 【HDU - 3533】Escape(bfs)

    Escape  Descriptions: 一个人从(0,0)跑到(n,m),只有k点能量,一秒消耗一点,在图中有k个炮塔,给出炮塔的射击方向c,射击间隔t,子弹速度v,坐标x,y问这个人能不能安全到 ...

  5. 【HDU - 6581】Vacation(思维)

    Vacation 题意 有n+1辆车,属性有长度l,距离终点的距离s,速度v问你最末尾的车到达终点的时间 Sample Input 1 2 2 7 1 2 1 2 1 2 2 10 7 1 6 2 1 ...

  6. 【HDU 5750】Dertouzos(数学)

    题目给定n和d,都是10的9次方以内,求1到n里面有几个数最大因数是d?1000000组数据.解:求出d的满足p[i]*d<n的最小质因数是第几个质数.即为答案. #include<cst ...

  7. 【HDU 2955】Robberies(DP)

    题意是给你抢劫每个银行可获得的钱m和被抓的概率p,求被抓的概率小于P,最多能抢多少钱.01背包问题,体积是m,价值是p.被抓的概率不是简单相加,而应该是1−Π(1−p[i])DP:dp[i]表示抢到i ...

  8. 【HDU 6000】Wash(贪心)

    Problem Description Mr.Panda is about to engage in his favourite activity doing laundry! He's brough ...

  9. 【UOJ#311】【UNR #2】积劳成疾(动态规划)

    [UOJ#311][UNR #2]积劳成疾(动态规划) UOJ Solution 考虑最大值分治解决问题.每次枚举最大值所在的位置,强制不能跨过最大值,左右此时不会影响,可以分开考虑. 那么设\(f[ ...

随机推荐

  1. PHPmail 亲测可用

    2017年5月8日9:10:47 1.在模块的配置文件中加入下里面代码,账号最好用126邮箱'THINK_EMAIL' => array( 'SMTP_HOST' => 'smtp.163 ...

  2. 交表(Send a Table)

    #include<stdio.h> #include<string.h> #define N 50010 int phi[N],n,sum[N]; void phi_table ...

  3. GCD Counting Codeforces - 990G

    https://www.luogu.org/problemnew/show/CF990G 耶,又一道好题被我浪费掉了,不会做.. 显然可以反演,在这之前只需对于每个i,统计出有多少(x,y),满足x到 ...

  4. list的一些功能

    x = [1,5,2,3,4] 1.列表反转序: 函数法: x.reverse()该方法没有返回值但会对列表进行反向排序. 注意 不能y=x.reverse(),会得到None 如果要的话要y=rev ...

  5. J2sdk中的主要的包介绍

  6. js数组去重的三种方式的比较

    做前端的,一般实现功能是主要的,但是重中之重却是在做到功能完善的情况下提高性能. 1.遍历数组法 实现的思路:构建一个新的数组存放结果,for循环中每次从原数组中取出一个元素,用这个元素循环与结果数组 ...

  7. 关于发布WP 8.1应用信息不匹配问题的解决办法

    错误提示:   与此更新关联的程序包标识符与已上传程序包中的标识符不匹配: The package identity associated with this update doesn't match ...

  8. jquery实现上传图片及图片大小验证、图片预览效果代码

    jquery实现上传图片及图片大小验证.图片预览效果代码 jquery实现上传图片及图片大小验证.图片预览效果代码 上传图片验证 */ function submit_upload_picture() ...

  9. 使用原生JavaScript模拟getElementByClassName .

    最近在工作中,由于有一个插件必须使用jquery-pack.js,而这个包又是非常古老的jquery,所以又的函数是无法使用的,例如$()选择器以及parent()都取不到标签的内容. 所以没办法,只 ...

  10. qt5.8+vs2015使用Qt5WebEngine搭建环境

    转载请注明出处:http://www.cnblogs.com/dachen408/p/7575094.html 1.项目属性,C/C++,所有选项,附加包含目录新增. $(QTDIR)\include ...