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.
 

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   |   We have carefully selected several similar problems for you:  6018 6017 6016 6015 6014 
 

Statistic | Submit | Discuss | Note

2 0
原创粉丝点击