Networking
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7753   Accepted: 4247

Description

You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area. 
Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.

Input

The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line. 
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i. 

Output

For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.

Sample Input

1 0

2 3
1 2 37
2 1 17
1 2 68 3 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 32 5 7
1 2 5
2 3 7
2 4 8
4 5 11
3 5 10
1 5 6
4 2 12 0

Sample Output

0
17
16
26
赤裸裸的最小生成树问题,下面用三种方式分别实现。
/*
Kruskal 1287 Accepted 420K 16MS G++
*/
#include"cstdio"
#include"algorithm"
using namespace std;
const int MAXN=;
struct Edge{
int u,v,cost;
}es[MAXN];
bool comp(const Edge &a,const Edge &b)
{
return a.cost < b.cost;
} int par[MAXN];
int rnk[MAXN];
void init(int n)
{
for(int i=;i<=n;i++)
{
par[i]=i;
}
} int fnd(int x)
{
if(par[x]==x)
{
return x;
}
return par[x]=fnd(par[x]);
} void unite(int u,int v)
{
int a=fnd(u);
int b=fnd(v);
if(a==b) return ; if(rnk[a]<rnk[b])
{
par[a]=b;
}
else{
par[b]=a;
if(rnk[a]==rnk[b]) rnk[a]++;
}
} bool same(int u,int v)
{
return fnd(u)==fnd(v);
} int main()
{
int V,E;
while(scanf("%d",&V)!=EOF&&V)
{
scanf("%d",&E);
for(int i=;i<E;i++)
{
scanf("%d%d%d",&es[i].u,&es[i].v,&es[i].cost);
}
sort(es,es+E,comp);
init(V);
int ans=;
int cnt=;
for(int i=;i<E;i++)
{
if(!same(es[i].u,es[i].v))
{
unite(es[i].u,es[i].v);
ans+=es[i].cost;
cnt++;
}
if(cnt==V-) break;
}
printf("%d\n",ans);
}
return ;
}
/*
Prim 1287 Accepted 408K 16MS G++
*/
#include"cstdio"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
int mp[MAXN][MAXN];
int V,E;
inline int min(int a,int b)
{
return a>b?b:a;
}
int d[MAXN];
int vis[MAXN];
int prim(int s)
{
int ans=;
for(int i=;i<=V;i++)
{
d[i]=mp[s][i];
vis[i]=;
}
d[s]=,vis[s]=;
for(int i=;i<=V-;i++)
{
int mincost,k;
mincost=INF; for(int i=;i<=V;i++)
{
if(!vis[i]&&mincost>d[i])
{
mincost=d[i];
k=i;
}
}
vis[k]=;
ans+=mincost;
for(int i=;i<=V;i++)
{
if(!vis[i]&&d[i]>mp[k][i])
{
d[i]=mp[k][i];
}
}
}
return ans;
} int main()
{
while(scanf("%d",&V)&&V)
{
for(int i=;i<=V;i++)
for(int j=;j<=V;j++)
if(i==j) mp[i][j]=;
else mp[i][j]=INF;
scanf("%d",&E);
for(int i=;i<E;i++)
{
int u,v,co;
scanf("%d%d%d",&u,&v,&co);
mp[u][v]=mp[v][u]=min(mp[u][v],co);
}
printf("%d\n",prim());
} return ;
}
/*
堆优化prim 1287 Accepted 604K 16MS G++
*/
#include"cstdio"
#include"cstring"
#include"queue"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
struct Edge{
int to,cost,next;
}es[MAXN];
struct Node{
int d,u;
Node(int cd,int cu):d(cd),u(cu){ }
bool operator<(const Node& a) const
{
return d > a.d;
}
};
int V,E;
int head[];
int cnt;
void add_edge(int u,int v,int co)
{
es[cnt].to=v;
es[cnt].cost=co;
es[cnt].next=head[u];
head[u]=cnt;
cnt++;
}
int d[];
int vis[];
int prim(int s)
{
int ans=;
for(int i=;i<=V;i++)
{
vis[i]=;
d[i]=INF;
}
d[s]=;
priority_queue<Node> que;
que.push(Node(,s));
while(!que.empty())
{
Node no=que.top();que.pop();
int v=no.u;
if(vis[v]) continue;
ans+=no.d;
vis[v]=;
for(int i=head[v];i!=-;i=es[i].next)
{
Edge e=es[i];
if(d[e.to]>e.cost)
{
d[e.to]=e.cost;
que.push(Node(d[e.to],e.to));
}
}
}
return ans;
}
int main()
{
while(scanf("%d",&V)&&V)
{
cnt=;
memset(head,-,sizeof(head));
scanf("%d",&E);
for(int i=;i<E;i++)
{
int u,v,co;
scanf("%d%d%d",&u,&v,&co);
add_edge(u,v,co);
add_edge(v,u,co);
} int ans=prim();
printf("%d\n",ans);
}
return ;
}

POJ1287(最小生成树入门题)的更多相关文章

  1. hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  2. poj 2524:Ubiquitous Religions(并查集,入门题)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23997   Accepted:  ...

  3. poj 3984:迷宫问题(广搜,入门题)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description ...

  4. hdu 1754:I Hate It(线段树,入门题,RMQ问题)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. poj 3254 状压dp入门题

    1.poj 3254  Corn Fields    状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...

  6. zstu.4194: 字符串匹配(kmp入门题&& 心得)

    4194: 字符串匹配 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 206  Solved: 78 Description 给你两个字符串A,B,请 ...

  7. hrbustoj 1073:病毒(并查集,入门题)

    病毒Time Limit: 1000 MS Memory Limit: 65536 KTotal Submit: 719(185 users) Total Accepted: 247(163 user ...

  8. hdu 1465:不容易系列之一(递推入门题)

    不容易系列之一 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  9. hrbustoj 1545:基础数据结构——顺序表(2)(数据结构,顺序表的实现及基本操作,入门题)

    基础数据结构——顺序表(2) Time Limit: 1000 MS    Memory Limit: 10240 K Total Submit: 355(143 users) Total Accep ...

随机推荐

  1. Linux内核源码分析方法_转

    Linux内核源码分析方法 转自:http://www.cnblogs.com/fanzhidongyzby/archive/2013/03/20/2970624.html 一.内核源码之我见 Lin ...

  2. uva 12083 Guardian of Decency (二分图匹配)

    uva 12083 Guardian of Decency Description Frank N. Stein is a very conservative high-school teacher. ...

  3. Redis源代码分析(十七)--- multi事务操作

    redis作为一非关系型数据库,居然相同拥有与RDBMS的事务操作,不免让我认为比較吃惊.在redis就专门有文件就是运行事务的相关操作的.也能够让我们领略一下.在Redis的代码中是怎样实现事务操作 ...

  4. EasyNVR RTSP转HLS(m3u8+ts)流媒体服务器前端构建之:bootstrap-datepicker日历插件的实时动态展现

    EasyNVR中有对录像进行检索回放的功能,且先抛开录像的回放,为了更好的用户体验过.让用户方便快捷的找到对应通道对应日期的录像视频,是必须的功能. 基于上述的需求,为前端添加一个日历插件,在日历上展 ...

  5. python之异步IO

    协程的用武之地 并发量较大的系统和容易在IO方面出现瓶颈(磁盘IO,网络IO),采用多线程.多进程可以解决这个问题,当然线程.进程的切换时很消耗资源的.最好的解决方案是使用单线程方式解决并发IO问题- ...

  6. S2S4H整合注意问题

    整合过程中出现问题记录: 1.The import javax.servlet.http.HttpServletRequest cannot be resolved 解决办法:在tomcat的lib目 ...

  7. ubuntu16.04+cuda8.0+cudnn5.0+caffe

    ubuntu安装过程(硬盘安装)http://www.cnblogs.com/zhbzz2007/p/5493395.html“但是千万不要用麒麟版!!!比原版体验要差很多!!!”开关机的时候电脑最上 ...

  8. matlab实战中一些重要的函数总结

    这段时间看了一些大型的matlabproject文件(如:faster r-cnn),对于project中常常要用到的一些函数进行一个总结. 1.路径问题. 这主要涵括文件路径的包括和组合. curd ...

  9. 修正IE6中FIXED不能用的办法,转载

    .fixed-top /* 头部固定 */{position:fixed;bottom:auto;top:0px;} .fixed-bottom /* 底部固定 */{position:fixed;b ...

  10. Linux就该这么学--命令集合11(配置系统相关信息)

    1.配置主机名称: 查看主机名: hostname 修改主机名: vim /etc/hostname 2.配置网卡信息: 在红帽RHEL6系统中网卡配置文件的前缀为“ifcfg-eth”,第一块即为“ ...