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. 转:/etc/inittab文件的字段及其说明

    /etc/inittab文件中每个登记项的结构都是一样的,共分为以冒号“:”分隔的4个字段.具体如下:       identifier :  run_level  :  action  :  pro ...

  2. UART RS232 的CTS与RTS

    目前较为常用的串口有9针串口(DB9)和25针串口(DB25),通信距离较近时(<12m),可以用电缆线直接连接标准RS232端口(RS422,RS485较远),若距离较远,需附加调制解调器(M ...

  3. 吃了单片机GPIO端口工作模式的大亏 ——关于强推挽输出和准双向口(弱上拉)的实际应用

    最近公司在进行一个项目,需要用到超声波测距的功能,于是在做好硬件电路,但在写控制程序时,却遇上了令我费解的事情. 当在单片机最小系统上调好输出频率40kHz,占空比50%的方波输出信号后,将程序烧至超 ...

  4. ebtablesBridge

    ebtables和iptables类似,都是Linux系统下网络数据包过滤的配置工具.既然称之为配置工具,就是说过滤功能是由内核底层提供支持的,这两个工具只是负责制定过滤的rules. ebtable ...

  5. 转:理解Java泛型

    JDK 5.0 中增加的泛型类型,是 Java 语言中类型安全的一次重要改进.但是,对于初次使用泛型类型的用户来说,泛型的某些方面看起来可能不容易明白,甚至非常奇怪.在本月的“Java 理论和实践”中 ...

  6. Jenkins的错误“error fetching remote repo origin”的问题解决

    错误如上,解决方法收集,可以尝试以下方法: http://stackoverflow.com/questions/38391601/jenkins-error-error-fetching-remot ...

  7. python_Opencv_读取视频

    目标 • 学会读取视频文件,显示视频,保存视频文件 • 学会从摄像头获取并显示视频 • 你将会学习到这些函数:cv2.VideoCapture(),cv2.VideoWrite()用摄像头捕获视频 使 ...

  8. floor() 和 ceil()函数

    在C语言的库函数中,floor函数的语法例如以下: #include <math.h> double floor( double arg ); 功能: 函数返回參数不大于arg的最大整数. ...

  9. 日积月累:ProguardGui进行jar包代码混淆

    前面文章<Proguard进行源代码混淆>讲解过怎么使用Proguard工具对Android的源代码进行混淆的方法(感兴趣的朋友可以访问:http://blog.csdn.net/p106 ...

  10. PHP5.4新特性(转)

    PHP5.4正式前两天发布了,之前有看了一些PHP5.4主要特性相关文章,因此在这里小结一下. 其中好几点更新是由Laruence贡献的!本文部分内容也是源自Laruence的博客. 1. Buid- ...