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

  1. 2
  2. 5
  3. 0 0 0 1 2
  4. 0 0 1 0 2
  5. 0 1 1 1 2
  6. 1 0 1 1 2
  7. 1 0 0 1 5
  8. 9
  9. 1 1 3 1 1
  10. 1 1 1 3 2
  11. 3 1 3 3 2
  12. 1 3 3 3 1
  13. 1 1 2 2 2
  14. 2 2 3 3 3
  15. 3 1 2 2 1
  16. 2 2 1 3 2
  17. 4 1 5 1 4

Sample Output

  1. Case #1: 8
  2. Case #2: 4

Source


2016 CCPC-Final

题解


题意:给出一个无向图,问其中的最小简单环(经过每条边仅一次),若找不到环,输出0。

枚举每一条边,删除它,再求这两点之间的最短路即可。

当然,直接这么做,时间\(O(m(n+m)logn)\),因此需要剪枝:

若当前最小环值为ans,那么在最短路过程中,当前最小边的长度大于ans,直接退出即可。

参考代码

  1. #include <map>
  2. #include <queue>
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <complex>
  6. #include <cstring>
  7. #include <cstdlib>
  8. #include <iostream>
  9. #include <algorithm>
  10. #define ll long long
  11. #define inf 5000000000LL
  12. #define PI acos(-1)
  13. #define REP(i,x,n) for(int i=x;i<=n;i++)
  14. #define DEP(i,n,x) for(int i=n;i>=x;i--)
  15. #define mem(a,x) memset(a,x,sizeof(a))
  16. using namespace std;
  17. ll read(){
  18. ll x=0,f=1;char ch=getchar();
  19. while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
  20. while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
  21. return x*f;
  22. }
  23. void Out(ll a){
  24. if(a<0) putchar('-'),a=-a;
  25. if(a>=10) Out(a/10);
  26. putchar(a%10+'0');
  27. }
  28. const int N=80005;
  29. map<int,map<int,int> >pos,vis;
  30. int sz;
  31. int find(int x,int y){
  32. if(!pos[x][y]) pos[x][y]=++sz;
  33. return pos[x][y];
  34. };
  35. struct node{
  36. int to,nxt;
  37. ll cost;
  38. node(){}
  39. node(int s1,int s2,ll s3){
  40. to=s1;nxt=s2;cost=s3;
  41. }
  42. bool operator < (const node &an) const{
  43. return cost>an.cost;
  44. }
  45. }Path[N];
  46. int head[N],e;
  47. void Addedge(int u,int v,int w){
  48. Path[++e]=node(v,head[u],(ll)w);
  49. head[u]=e;
  50. }
  51. void Init(){
  52. sz=0;e=0;
  53. mem(head,0);
  54. pos.clear();
  55. vis.clear();
  56. }
  57. ll dis[N],ans;
  58. bool book[N];
  59. ll Dijkstra(int s,int t,ll w){
  60. priority_queue<node>que;
  61. REP(i,0,sz) dis[i]=inf;
  62. mem(book,false);
  63. dis[s]=0;
  64. que.push(node(s,-1,0));
  65. int u,v;
  66. struct node cur;
  67. while(!que.empty()){
  68. cur=que.top();
  69. que.pop();u=cur.to;
  70. if(cur.cost>=w) break;;
  71. if(book[u]) continue;
  72. book[u]=true;
  73. for(int i=head[u];i;i=Path[i].nxt){
  74. v=Path[i].to;
  75. if(dis[v]>dis[u]+Path[i].cost){
  76. dis[v]=dis[u]+Path[i].cost;
  77. que.push(node(v,-1,dis[v]));
  78. }
  79. }
  80. }
  81. return dis[t];
  82. }
  83. int main(){
  84. int T=read();
  85. REP(i,1,T){
  86. Init();
  87. int m=read();
  88. int u,v,w,x1,y1,x2,y2;
  89. REP(i,1,m){
  90. x1=read();y1=read();
  91. x2=read();y2=read();
  92. w=read();
  93. u=find(x1,y1);v=find(x2,y2);
  94. Addedge(u,v,w);
  95. Addedge(v,u,w);
  96. }
  97. ans=inf;ll tmp;
  98. REP(i,1,sz){
  99. for(int k=head[i];k;k=Path[k].nxt){
  100. u=i;v=Path[k].to;
  101. if(vis[u][v]||vis[v][u]) continue;
  102. vis[u][v]=vis[v][u]=1;
  103. tmp=Path[k].cost;
  104. Path[k].cost=inf;
  105. ans=min(ans,Dijkstra(u,v,ans-tmp)+tmp);
  106. Path[k].cost=tmp;
  107. }
  108. }
  109. printf("Case #%d: %lld\n",i,ans==inf?0:ans);
  110. }
  111. return 0;
  112. }

【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. C++ 的浅拷贝和深拷贝(结构体)

    关于浅拷贝和深拷贝这个问题遇上的次数不多,这次遇上整理一下,先说这样一个问题,关于浅拷贝的问题,先从最简单的说起. 假设存在一个结构体: struct Student { string name; i ...

  2. 用jQuery获取到一个类名获取到的是一个数组 ,如果对数组中的每个进行相应的操作可以这样进行

    $(".userImg").each(function(){ $(this).click(function(){ var imgid = $(this).attr("id ...

  3. [APIO2012]派遣 洛谷P1552 bzoj2809 codevs1763

    http://www.codevs.cn/problem/1763/ https://www.lydsy.com/JudgeOnline/problem.php?id=2809 https://www ...

  4. MySQL防止重复插入记录SQL

    INSERT INTO sysuser(') DUAL:表示虚拟表 sysuser:表名称

  5. Win10新机的安装与配置

    一.快捷键 打开Chrome上次关闭的所有标签页:Ctrl-Shift-T 二.问题解决 1. 右键取得管理员权限 https://www.tenforums.com/tutorials/3841-a ...

  6. 【转】一篇文章,教你学会Git

    一篇文章,教你学会Git 在日常工作中,经常会用到Git操作.但是对于新人来讲,刚上来对Git很陌生,操作起来也很懵逼.本篇文章主要针对刚开始接触Git的新人,理解Git的基本原理,掌握常用的一些命令 ...

  7. PKU_campus_2018_D Chocolate

    思路: 题目链接http://poj.openjudge.cn/practice/C18D/ kruskal过程中使用乘法原理计数. 实现: #include <bits/stdc++.h> ...

  8. Backbone.js之Todo源码浅析

    相信每个接触了解过backbone的人都知道todo,网上的关于它的分析教程也都分析乱了.但是,知识只有自己学习领悟才是自己的,话不多说,正文开始. 在分析todo的源码之前,首先我们要知道todo具 ...

  9. Java 11 正式发布,支持期限至2026年9月

    美国当地时间9月25日,Oracle 官方宣布 Java 11 (18.9 LTS) 正式发布,可在生产环境中使用!这是自 Java 8 后的首个长期支持版本,非常值得大家的关注,可以通过下面的地址进 ...

  10. 在phpnow中配置phpunit

    前面都好了之后,在 D:\phpnow\php-5.2.14-Win32\PEAR 之外的地方执行 phpinfo 都会出现以下错误 Warning: require_once(File/Iterat ...