HDU 2480 - Steal the Treasure(贪心+并查集)

Steal the Treasure

Time Limit: 10000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description

The alliance of thieves decides to steal the treasure from country A. There are n cities in country A. Cities are connected by directional or bidirectional road. To avoid the risk, the king of country A divides his treasure and hides
them in some place on the road.

The alliance has found out the secret of the king. They get a map of country A which shows the location and the quantity of treasure on each road. In order to make the maximum profit and reduce the least loss, the alliance determines to send n thieves respectively
to each city (one city one thief). At the appointed time, each thief chooses one road (if there is a road and notice that the road may have direction) to get to its corresponding city. Then he can steal the treasure on that road. After stealing, all the thieves
return back to their base immediately.

The heads of the alliance wonder to know the quantity of the treasure they can steal at most.

Input

There are multiple cases. Input is terminated by EOF.

For each case, the first line contains two integers n (1<=n<=1000) and m (0<=m<=n*(n-1)/2), representing the number of cities and the number of roads in country A. The following m lines, each line contains four integers x, y (1<=x, y<=n, x≠y), d (0<=d<=1),
w (0<=w<=1000), which means that there is a road from city x to city y, d=0 shows this road is bidirectional and d=1 shows it is directional and x the starting point, w is the quantity of treasure on the road.

We guarantee that the road (x, y) and (y, x) will never appear together in the same case.

Output

For each case, output the maximum quantity of treasure the alliance can get.

Sample Input

2 1
1 2 0 10
5 5
1 2 1 0
1 3 1 10
2 3 0 20
3 4 0 30
4 2 1 40

Sample Output

10
100

题意:有 n 个城市, m 条边,每条边连接两个城市。x y d w 表示,x y 之间有一条边,边上有财富值 w ,d 为 1 表示有向边, d 为 0 表示 无向边。小偷集团要窃取这些财富值,在每个 城市都有一个小偷,经过相邻的边时就会得到该条边对应的财富值。每条边只可以走一次。问最后可以得到的最大值。

思路:贪心,按边权从大到小排序。对于有向边,只能选起点,那无向边呢,两个点都是可以的,而且怎么选择对后面是有影响的。于是想到先把这两个点标记为待确定点,根绝后面的选择再确定这两个点。注意到一个点可能是多条无向边的端点,也就是说可能有许多点互相关联,待确定,只要其中有一个点被确定了,那么其他所有的点就都被确定好了。这样这些点构成了一个联通块,可以用并查集实现。这样贪心的时候,每个联通块缩为一个点,每个点的状态有已被选,和未选,再根据相应的策略去贪心就好了。

<span style="font-size:18px;">#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;

const double PI = acos(-1.0);
const double e = 2.718281828459;
const double eps = 1e-8;
const int MAXN = 1010;
int father[MAXN];
struct Edge
{
    int u, v;
    int d, w;
} edge[MAXN*MAXN/2];
int vis[MAXN];
int n, m;

int Find(int x)
{   // 路径压缩
    return (x==father[x])?x:(father[x]=Find(father[x]));
}

int cmp(Edge a, Edge b)
{
    return a.w > b.w;
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    while(cin>>n>>m)
    {
        for(int i = 1; i <= m; i++)
        {
            scanf("%d %d %d %d", &edge[i].u, &edge[i].v, &edge[i].d, &edge[i].w);
        }
        sort(edge+1, edge+1+m, cmp);
        for(int i = 1; i <= n; i++)
            father[i] = i;
        memset(vis, 0, sizeof(vis));
        int ans = 0;
        for(int i = 1; i <= m; i++)
        {
            int x = Find(edge[i].u);
            int y = Find(edge[i].v);
            if(vis[x] && vis[y])
                continue;
            if(edge[i].d==1 && vis[x])
                continue;
            ans += edge[i].w;
            if(edge[i].d == 1)
                vis[x] = 1;
            else
            {
                if(x == y)
                    vis[x] = 1;
                if(!vis[x] && !vis[y])
                    father[x] = y;
                else if(vis[x])
                    vis[y] = 1;
                else if(vis[y])
                    vis[x] = 1;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}</span>
时间: 2024-10-21 02:07:22

HDU 2480 - Steal the Treasure(贪心+并查集)的相关文章

HDU 3047 Zjnu Stadium 带权并查集

题目来源:HDU 3047 Zjnu Stadium 题意:给你一些人 然后每次输入a b c 表示b在距离a的右边c处 求有多少个矛盾的情况 思路:用sum[a] 代表a点距离根的距离 每次合并时如果根一样 判断sum数组是否符合情况 根不一样 合并两棵树 这里就是带权并查集的精髓 sum[y] = sum[a]-sum[b]+x 这里y的没有合并前b的根 #include <cstdio> #include <cstring> using namespace std; cons

贪心 + 并查集 之 CODE[VS] 1069 关押罪犯 2010年NOIP全国联赛提高组

/* 贪心 + 并查集 之 CODE[VS] 1069 关押罪犯  2010年NOIP全国联赛提高组 两座监狱,M组罪犯冲突,目标:第一个冲突事件的影响力最小. 依据冲突大小,将M组罪犯按从大到小排序,按照排序结果,依次把每组罪犯分开放入两个监狱, 直到当前这组罪犯已经在同一个监狱中了,此时即为答案. 实现: 1)通过不在同一个监狱的罪犯,推断出在同一个监狱的罪犯.(依据:一共就两个监狱)      ftr[b] = a+n   // a和b是在不同监狱 ftr[c] = a+n   // a和

hdu 1829 A Bug&#39;s Life 并查集系列

1 #include "cstdio" 2 #include "iostream" 3 #include "cstring" 4 #include "vector" 5 #include "queue" 6 using namespace std; 7 8 #define MAXN 2222 9 int fa[MAXN]; 10 int rnk[MAXN]; //秩 表示某点与根的距离 11 int n,

HDU 3367 Pseudoforest(伪森林)(并查集)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3367 题意:在图论中,如果一个森林中有很多连通分量,并且每个连通分量中至多有一个环,那么这个森林就称为伪森林. 现在给出一个森林,求森林包含的最大的伪森林,其大小通过所有边的权值之和来比较. 分析: 1.一开始想的是:在每个连通分量中求一个最大生成树,然后加一条最大的边,再把每个连通分量算出来的值加起来,但WA了.这并不是最优的,因为还存在这种情况:一个连通分量里最初有两个环,但是伪森林要求最多一个

hdu 1272 小希的迷宫(简单并查集)

小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 31396    Accepted Submission(s): 9726 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是

POJ 2492 || HDU 1829:A Bug&#39;s Life(并查集)

传送门: POJ:点击打开链接 HDU:点击打开链接 A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 27624   Accepted: 8979 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they fe

HDU 1232:畅通问题(并查集)

畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29362    Accepted Submission(s): 15452 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通(但不一定有

poj 1456 Supermarket (贪心+并查集)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int fa[10010]; struct node { int p; int d; }; struct node a[10010]; bool cmp(node a1,node a2)//利润从大到小 { return a1.p>a2.p; } int find(int x) { if(fa[x]

Codeforces 437D The Child and Zoo(贪心+并查集)

题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去参观动物园,动物园分很多个区,每个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路的权值为该条路连接的两个区中权值较小的一个.如果两个区没有直接连接,那么f值即为从一个区走到另一个区中所经过的路中权值最小的值做为权值.问,平均两个区之间移动的权值为多少. 解题思路:并查集+贪心.将所有的边按照权值排序,从最大的开始连接,每次连接时计算的次数为连接两块的节点数的积(乘法原理). #in