Steal the Treasure

Time Limit: 10000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 775    Accepted Submission(s): 213

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条道路连通,每一条道路都有着一定的权值(财富),这些道路有的是可以双向连通的,有的是单向的,只能从一个点出发,现在每一个城市都有着一个小偷,在特定时刻,这些小偷可以从这个城市前往任一条道路(如果可以),并拿走这条道路上的财富,现在问你最多能拿到多少财富。
思路分析:首先肯定是贪心,将边按照边权从大到小排序,到底能不能拿这个路上的财富是由他的端点城市决定的,对于有向边,如果起点未被标记,那么就拿走财富,标记起点,对于无向边,如果有一个点没被标记,就标记那个点,否则就用并查集将其缩为一点。
代码:
#include <iostream>
#include<cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=+;
struct node
{
int x,y;
int d,w;
};
node edge[maxn*maxn/];
int fa[maxn];
int root(int x)
{
return (x==fa[x])?x:fa[x]=root(fa[x]);
}
bool cmp(node a,node b)
{
return a.w>b.w;
}
bool vis[maxn];
int n,m;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++)
fa[i]=i;
for(int i=;i<m;i++)
{
scanf("%d%d%d%d",&edge[i].x,&edge[i].y,&edge[i].d,&edge[i].w);
}
sort(edge,edge+m,cmp);
int ans=;
for(int i=;i<m;i++)
{
int fx=root(edge[i].x);
int fy=root(edge[i].y);
if(vis[fx]&&vis[fy]) continue;
if(edge[i].d==&&vis[fx]) continue;
ans+=edge[i].w;
if(edge[i].d==) vis[fx]=true;
else
{
if(fx==fy) vis[fx]=true;
else if(vis[fx]) vis[fy]=true;
else if(vis[fy]) vis[fx]=true;
else fa[fx]=fy;//缩点
}
}
printf("%d\n",ans);
}
}

hdu 2480 贪心+简单并查集的更多相关文章

  1. 1213 How Many Tables(简单并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...

  2. POJ 2524 (简单并查集) Ubiquitous Religions

    题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...

  3. poj1611 简单并查集

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 32781   Accepted: 15902 De ...

  4. 【简单并查集】Farm Irrigation

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

  5. Luogu 1525 【NOIP2010】关押罪犯 (贪心,并查集)

    Luogu 1525 [NOIP2010]关押罪犯 (贪心,并查集) Description S城现有两座监狱,一共关押着N名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨 ...

  6. HDU 1811 拓扑排序 并查集

    有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...

  7. hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)

    hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...

  8. ACM_“打老虎”的背后(简单并查集)

    “打老虎”的背后 Time Limit: 2000/1000ms (Java/Others) Problem Description: “习大大”自担任国家主席以来大力反腐倡廉,各地打击贪腐力度也逐步 ...

  9. 简单并查集 -- HDU 1232 UVALA 3644 HDU 1856

    并查集模板: #include<iostream> using namespace std; ],x,y; ]; //初始化 x 集合 void init(int n) { ; i< ...

随机推荐

  1. codevs 4310 复制书稿

    4310 复制书稿  时间限制: 1 s  空间限制: 4000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 现在要把m本有顺序的书分给k个人 ...

  2. Solr4.8.0源码分析(17)之SolrCloud索引深入(4)

    Solr4.8.0源码分析(17)之SolrCloud索引深入(4) 前面几节以add为例已经介绍了solrcloud索引链建索引的三步过程,delete以及deletebyquery跟add过程大同 ...

  3. js中undefined,null,NaN的区别

    1.类型分析: js中的数据类型有undefined,boolean,number,string,object等5种,前4种为原始类型,第5种为引用类型.var a1;var a2 = true;va ...

  4. C# POST数据到指定页面,并跳转至该页面

    /// <summary> /// 跨页面POST数据 /// </summary> public class RemotePost : Dictionary<strin ...

  5. C++ 通过Thunk在WNDPROC中访问this指针

    本文基本只讨论原理,具体实现请参见后续文章<C++ 通过Thunk在WNDPROC中访问this指针实现细节> 当注册窗口类时,WNDCLASSEX结构的lpfnWndProc成员应设置为 ...

  6. selenium webdriver python 元素操作

    常用操作 click 点击某个元素 driver.find_element_by_id(“su”).click()   clear driver.find_element_by_id(“kw”).cl ...

  7. MVC - 知识点

    1.     @Styles.Render("~/Content/css")  是怎么工作的? 在App_Start文件夹里面的BundleConfig.cs中定义了StyleBu ...

  8. 多目标遗传算法 ------ NSGA-II (部分源码解析) 交叉操作 crossover.c

    遗传算法中的交叉操作是 对NSGA-II  源码分析的  最后一部分, 这一部分也是我 从读该算法源代码和看该算法论文理解偏差最大的  函数模块. 这里,首先提一下,遗传算法的  交叉操作.变异操作都 ...

  9. Java多线程编程(一)

    1.Java创建多线程的方法一:(1)实现Runnable接口并实现其中的run()方法:(2)将Runable对象提交给一个Thread构造器,调用start()方法. [程序实例]单线程 publ ...

  10. AFNetworking (3.1.0) 源码解析 <一>

    首先说一下AFNetworking的github地址:GitHub - AFNetworking/AFNetworking: A delightful networking framework for ...