2017ACM/ICPC廣西邀請賽-重現賽 hdu 6187

 

Long times ago, there are beautiful historic walls in the city. These walls divide the city into many parts of area. 

Since it was not convenient, the new king wants to destroy some of these walls, so he can arrive anywhere from his castle. We assume that his castle locates at (0.6∗2‾√,0.6∗3‾√)(0.6∗2,0.6∗3). 

There are nn towers in the city, which numbered from 1 to n. The ith's location is (xi,yi)(xi,yi). Also, there are m walls connecting the towers. Specifically, the ith wall connects the tower uiui and the tower vivi(including the endpoint). The cost of destroying the ith wall is wiwi. 

Now the king asks you to help him to divide the city. Firstly, the king wants to destroy as less walls as possible, and in addition, he wants to make the cost least. 

The walls only intersect at the endpoint. It is guaranteed that no walls connects the same tower and no 2 walls connects the same pair of towers. Thait is to say, the given graph formed by the walls and towers doesn't contain any multiple edges or self-loops. 

Initially, you should tell the king how many walls he should destroy at least to achieve his goal, and the minimal cost under this condition. 

Input

There are several test cases. 

For each test case: 

The first line contains 2 integer n, m. 

Then next n lines describe the coordinates of the points. 

Each line contains 2 integers xi,yixi,yi. 

Then m lines follow, the ith line contains 3 integers ui,vi,wiui,vi,wi 

|xi|,|yi|≤105|xi|,|yi|≤105 

3≤n≤100000,1≤m≤2000003≤n≤100000,1≤m≤200000 

1≤ui,vi≤n,ui≠vi,0≤wi≤100001≤ui,vi≤n,ui≠vi,0≤wi≤10000 

Output

For each test case outout one line with 2 integers sperate by a space, indicate how many walls the king should destroy at least to achieve his goal, and the minimal cost under this condition. 

Sample Input

4 4
-1 -1
-1 1
1 1
1 -1
1 2 1
2 3 2
3 4 1
4 1 2

Sample Output

1 1

就是求個最大生成樹,用總的權值減去最大生成樹的值就是最小花費

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=1e7+5;
int fa[maxn],cnt,n,m;
inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
inline void init(){for(int i=1;i<=n;i++)fa[i]=i;}
struct edge{int u,v,w;}G[maxn];
inline int kruskal()
{
	for(int i=1;i<=n;i++)fa[i]=i;cnt=0;
	int ans=0;sort(G,G+m,[](const edge &a,const edge &b){return a.w>b.w;});
	for(int i=0;i<m;i++)
	{
		if(find(G[i].u)!=find(G[i].v)){fa[find(G[i].u)]=find(G[i].v);ans+=G[i].w;cnt++;if(cnt==n-1) break;}
	}
	return ans;
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int sum=0;
		for(int i=0;i<n;i++){int a,b;scanf("%d%d",&a,&b);}
		for(int i=0;i<m;i++)
		{
			scanf("%d%d%d",&G[i].u,&G[i].v,&G[i].w);sum+=G[i].w;
		}
		printf("%d %d\n",m-cnt,sum-kruskal());
	}
	return 0;
} 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章