HDU 6005 Pandaland(dijkstra + 剪枝)
来源:互联网 发布:淘宝网 欧瑞斯的价格 编辑:程序博客网 时间:2024/05/29 17:33
题意:
给你一个m 个边的无向图,要求在图上找一个最小的环(边权)。
思路:
好暴力的一道题目。
一个环的话 肯定要经过一个边,我们直接暴力枚举哪一个边。
把这个边的权值设成inf。
然后跑这两个点的最短路即可。
加上这个边原权值 即是这个最小环。
注意加个剪枝:
当优先队列中的最小值大于等于ans 时 直接不跑了
#include <cstdio>#include <cstring>#include <algorithm>#include <map>#include <vector>#include <queue>using namespace std;int T,ks, n, cnt;const int maxn = 10000;const int inf = 0x3f3f3f3f;struct Edge{ int f,t,w; Edge(int f = 0, int t = 0,int w = 0):f(f),t(t),w(w){}};struct Node{ int u,d; bool operator < (const Node& rhs) const { return d > rhs.d; } Node(int u = 0, int d = 0):u(u),d(d){}};int ans;struct DDDDDD{ int n, m; vector<int>g[maxn]; vector<Edge> edges; int d[maxn]; bool vis[maxn]; priority_queue<Node>q; void init(int n){ this->n = n; m = 0; for (int i = 0; i <= n; ++i) g[i].clear(); edges.clear(); } void add(int f,int t,int w){ edges.push_back(Edge(f,t,w)); g[f].push_back(m++); } void dij(int s,int t,int oo){ for (int i = 0; i <= n; ++i) d[i] = inf,vis[i] = 0; d[s] = 0; q.push(Node(s,0)); while(!q.empty()){ Node nod = q.top(); q.pop(); int u = nod.u, dis = nod.d; if (dis+oo >= ans) break; if (vis[u])continue; vis[u] = 1; for (int i = 0; i < g[u].size(); ++i){ int v = g[u][i]; Edge& e = edges[v]; if (d[e.t] > d[u] + e.w){ d[e.t] = d[u] + e.w; q.push(Node(e.t,d[e.t])); } } } ans = min(ans, d[t] + oo); }}dic;map<pair<int,int>, int>mp;int ID(pair<int,int> a){ if (!mp.count(a)) return mp[a] = ++cnt; return mp[a];}int main(){ scanf("%d",&T); while(T--){ scanf("%d",&n); mp.clear(); dic.init(n*2+10); cnt = 0; for (int i = 0; i < n; ++i){ int x,y,x1,y1,w; scanf("%d %d %d %d %d",&x, &y, &x1, &y1,&w); int id1 = ID(make_pair(x,y)); int id2 = ID(make_pair(x1,y1)); dic.add(id1,id2,w); dic.add(id2,id1,w); } ans = inf; for (int i = 0; i < dic.edges.size(); i+=2){ Edge& e = dic.edges[i]; Edge& ee = dic.edges[i^1]; int oo = e.w; e.w = inf; ee.w = inf; dic.dij(e.f,e.t,oo); e.w = oo; ee.w = oo; } if (ans >= inf) ans = 0; printf("Case #%d: %d\n",++ks,ans); } return 0;}/**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 4ans =Case #1: 8Case #2: 4**/
Pandaland
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 105 Accepted Submission(s): 14
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.
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 integersx1,y1,x2,y2, w, representing there is a road with cost w connecting the cities on (x1,y1) and (x2,y2).
Each test case begins with an integer M.
Following M lines discribes roads in Pandaland.
Each line has 5 integers
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.
∙1≤T≤50.
∙1≤m≤4000.
∙−10000≤xi,yi≤10000.
∙1≤w≤105.
If there is no cycles in the map, y is 0.
limits
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 | We have carefully selected several similar problems for you: 6018 6017 6016 6015 6014
2 0
- HDU 6005 Pandaland(dijkstra + 剪枝)
- HDU 6005 Pandaland——dijkstra + 剪枝
- HDU 6005 Pandaland
- Hdu 6005 Pandaland 无向图最小环:最短路剪枝
- hdu 6005 Pandaland(dij+暴力)
- HDU 6005 Pandaland 最小环(最小生成树+LCA)
- Pandaland HDU
- Pandaland HDU
- HDU 6005 Pandaland[最小生成树][LCA]
- 【最小环 && 离散化】HDU 6005 Pandaland
- HDU 6005 Pandaland(无向图最小环)
- HDU6005 -dijkstra+剪枝
- hdu 1010 (DFS+剪枝)
- HDU 1010(剪枝+深搜)
- hdu 5305 (搜索+剪枝)
- hdu 1455 (DFS+剪枝)
- HDU 剪枝剪枝......
- hdu 2544 (dijkstra)
- 获取微信服务器ip
- [Warning] TIMESTAMP with implicit DEFAULT value is deprecated.
- 框架和类库的区别
- imessage使用二
- 没那么难,谈CSS的设计模式
- HDU 6005 Pandaland(dijkstra + 剪枝)
- JavaScript原型链继承的对比
- 图像信号的统计特性
- SEO Ultimate 7.6.5.9汉化版(中文版)免费下载
- Spring、SpringMVC、MyBatis、freeMaker框架下之Excel文件导出
- PL/SQL查询中文乱码问题
- 系列笔记11、观察者模式-ListView与Adapter
- SWT简介
- js实现简易的计算器