HNU ACM: Cheering up the Cows (kruscal)
来源:互联网 发布:基金挑选 知乎 编辑:程序博客网 时间:2024/05/02 06:24
B: Cheering up the Cows
Time Limit: 1000 ms Case Time Limit: 1000 ms Memory Limit: 65536 KB
Submit: 13 Accepted: 8
Description
Farmer John has grown so lazy that he no longer wants to continue maintaining the cow paths that currently provide a way to visit each of his N (5 <= N <= 10,000) pastures (conveniently numbered 1..N). Each and every pasture is home to one cow. FJ plans to remove as many of the P (N-1 <= P <= 100,000) paths as possible while keeping the pastures connected. You must determine which N-1 paths to keep.
Bidirectional path j connects pastures S_j and E_j (1 <= S_j <= N; 1 <= E_j <= N; S_j != E_j) and requires L_j (0 <= L_j <= 1,000) time to traverse. No pair of pastures is directly connected by more than one path.
The cows are sad that their transportation system is being reduced. You must visit each cow at least once every day to cheer her up. Every time you visit pasture i (even if you're just traveling
through), you must talk to the cow for time C_i (1 <= C_i <= 1,000).
You will spend each night in the same pasture (which you will choose) until the cows have recovered from their sadness. You will end up talking to the cow in the sleeping pasture at least in the morning when you wake up and in the evening after you have returned to sleep.
Assuming that Farmer John follows your suggestions of which paths to keep and you pick the optimal pasture to sleep in, determine the minimal amount of time it will take you to visit each cow at least once in a day.
Input
* Line 1: Two space-separated integers: N and P
* Lines 2..N+1: Line i+1 contains a single integer: C_i
* Lines N+2..N+P+1: Line N+j+1 contains three space-separated integers: S_j, E_j, and L_j
Output
* Line 1: A single integer, the total time it takes to visit all the cows (including the two visits to the cow in your sleeping-pasture)
Sample Input
5 71010206301 2 52 3 52 4 123 4 172 5 153 5 64 5 12
Sample Output
176
Hint
INPUT DETAILS:
+-(15)-+
/ /
/ /
1-(5)-2-(5)-3-(6)--5
/ /(17) /
(12)/ / /(12)
4------+
OUTPUT DETAILS:
Keep these paths:
1-(5)-2-(5)-3 5
/ /
(12)/ /(12)
*4------+
Wake up in pasture 4 and visit pastures in the order 4, 5, 4, 2, 3, 2, 1, 2, 4 yielding a total time of 176 before going back to sleep.
#include <stdio.h>
#include <string.h>
#include "algorithm"
#define maxe 100100
#define maxn 10100
using namespace std;
struct edge
{
int e,v;
int weight;
}path[maxe];
bool operator <(edge a,edge b)
{
return a.weight<b.weight;
}
int w[maxn];
int p[maxn],rank[maxn];
int n,m;
int find(int t)
{
if(t==p[t]) return p[t];
p[t]=find(p[t]);
return p[t];
}
void union_set(int x,int y)
{
int s=find(x),t=find(y);
if(rank[s]>rank[t]) p[t]=s;
else
{
p[s]=t;
if(rank[s]==rank[t]) rank[t]++;
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int small=0x3fffffff;
for(int i = 1;i<=n;i++)
{
scanf("%d",&w[i]);
if(small>w[i]) small=w[i];
p[i]=i;rank[i]=0;
}
for(int i=0;i<m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
path[i].e=x;path[i].v=y;
path[i].weight=z*2+w[x]+w[y];
}
sort(path,path+m);
int ans=0;int cnt=0;
for(int i=0;i<m;i++)
{
if(find(path[i].e)!=find(path[i].v))
{
ans+=path[i].weight;
cnt++;
union_set(path[i].e,path[i].v);
}
if(cnt==n-1) break;
}
printf("%d/n",ans+small);
}
return 0;
}
- HNU ACM: Cheering up the Cows (kruscal)
- Cheering up the Cows,2008nov,usaco
- 题解:cheering up the cows[USACO2008 NOV]
- 【USACO 2008 Nov Gold】 2.Cheering up the Cows 最小生成树、
- 洛谷 P2916 [USACO08NOV]为母牛欢呼Cheering up the C…
- 洛谷P2916 [USACO08NOV]为母牛欢呼Cheering up the C…
- acm hnu 10136 Palindromes
- acm HNU Dwarf Tower
- 2015 HNU Warm Up 01
- 2015 HNU warm up 02
- 2015 HNU warm up 03
- 2015 HNU Warm Up 04
- 2015 HNU Warm Up 06
- 2015 HNU warm up 05
- 2015 HNU warm up 07
- 2015 HNU warm up 12
- acm hnu Problem 10106 区间
- acm hnu 10049 IP Address
- D3DXCreateFontIndirect
- 在input的type="text"里控制只能输入数字
- Linux scp 命令
- 智能上海道闸特点
- 动态库的创建及使用
- HNU ACM: Cheering up the Cows (kruscal)
- 什么是IDOC,以及IDOC的步骤
- 网络编程常见问题总结
- 《Windows核心编程》---C运行库的内存对齐函数
- 我们的纪念日
- 多线程之volatile
- 《Symbian 因何没落》评论区诟詈公案后记
- Doxygen使用简介(3)
- API HOOK