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. C语言:Message类

    message.h #ifndef MESSAGE_H #define MESSAGE_H #define TRUE 1 #define FALSE 0 typedef struct { int se ...

  2. Red Hat TimesTen安装记录

    1:内核参数修改 # vi /etc/sysctl.conf kernel.sem= #sysctl –p 备注:此安装过程为测试环境,具体参数修改要参考TimesTen官方文档. 2:创建用户及组信 ...

  3. poj 2492A Bug's Life

    http://poj.org/problem?id=2492 #include<cstdio> #include<cstring> #include<algorithm& ...

  4. poj Building a Space Station

    http://poj.org/problem?id=2031 #include<cstdio> #include<cstring> #include<cmath> ...

  5. COJ 1011 WZJ的数据结构(十一)树上k大

    题解:主席树&DFS序. PS:为什么我一开始Wa了N发 是因为有一个左区间我写成[L,M+1]了.......................... #include<iostream ...

  6. Delphi7 THTTPRIO 控件设置超时

    HTTPRIOLeExp.HTTPWebNode.SendTimeout := 100000;  //发射  HTTPRIOLeExp.HTTPWebNode.ConnectTimeout := 10 ...

  7. A - ACM Computer Factory - poj 3436(最大流)

    题意:有一个ACM工厂会生产一些电脑,在这个工厂里面有一些生产线,分别生产不同的零件,不过他们生产的电脑可能是一体机,所以只能一些零件加工后别的生产线才可以继续加工,比如产品A在生产线1号加工后继续前 ...

  8. config large memory

    C Configuring Large Memory Optimization This appendix provides information for configuring memory op ...

  9. UVALive 4957 Fake scoreboard

    题意就是有n个队伍和m个题目 给出了每个队伍解决的题目数量 每个题目也给出了被解决的次数 然后求一个方阵. N,Y表示每个队伍是否过了哪个题目. 要求字典序最小. 这题给人的第一反应就是网络流. 虽然 ...

  10. POJ3273:Monthly Expense(二分)

    Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...