最小生成树prime+邻接表+优先队列
2016-04-14 16:04
134 查看
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
const long MAXN=10000;
struct Edge
{
long v;
long next;
long cost;
}e[MAXN];
struct node
{
long v;
long cost;
};
bool operator <(const node &a,const node &b)
{
return a.cost>b.cost;
}
priority_queue<node> q;
long p[MAXN],m,n,from,to,cost;
bool vist[MAXN];
void init()
{
memset(p,-1,sizeof(p));
memset(vist,0,sizeof(vist));
while (!q.empty()) q.pop();
long i,eid=0;
for (i=0; i<n; ++i)
{
scanf("%ld %ld %ld",&from,&to,&cost);
e[eid].next=p[from];
e[eid].v=to;
e[eid].cost=cost;
p[from]=eid++;
//以下适用于无向图
// swap(from,to);
// e[eid].next=p[from];
// e[eid].v=to;
// e[eid].cost=cost;
// p[from]=eid++;
}
}
void Prime()
{
long cost=0;
init();
node t;
t.v=from;//选择起点
t.cost=0;
q.push(t);
long tt=0;
while (!q.empty()&&tt<m)
{
t=q.top();q.pop();
if (vist[t.v]) continue;//由于加的时候可能加多、重,所以要跳过这个点
cost+=t.cost;
++tt;
vist[t.v]=true;
long j;
for (j=p[t.v]; j!=-1; j=e[j].next)//加下一堆边
{
if (!vist[e[j].v])
{
node temp;
temp.v=e[j].v;
temp.cost=e[j].cost;
q.push(temp);
}
}
}
printf("%ld\n",cost);
}
int main()
{
while (scanf("%ld%ld",&m,&n)!=EOF)
{
Prime();
}
return 0;
}
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
const long MAXN=10000;
struct Edge
{
long v;
long next;
long cost;
}e[MAXN];
struct node
{
long v;
long cost;
};
bool operator <(const node &a,const node &b)
{
return a.cost>b.cost;
}
priority_queue<node> q;
long p[MAXN],m,n,from,to,cost;
bool vist[MAXN];
void init()
{
memset(p,-1,sizeof(p));
memset(vist,0,sizeof(vist));
while (!q.empty()) q.pop();
long i,eid=0;
for (i=0; i<n; ++i)
{
scanf("%ld %ld %ld",&from,&to,&cost);
e[eid].next=p[from];
e[eid].v=to;
e[eid].cost=cost;
p[from]=eid++;
//以下适用于无向图
// swap(from,to);
// e[eid].next=p[from];
// e[eid].v=to;
// e[eid].cost=cost;
// p[from]=eid++;
}
}
void Prime()
{
long cost=0;
init();
node t;
t.v=from;//选择起点
t.cost=0;
q.push(t);
long tt=0;
while (!q.empty()&&tt<m)
{
t=q.top();q.pop();
if (vist[t.v]) continue;//由于加的时候可能加多、重,所以要跳过这个点
cost+=t.cost;
++tt;
vist[t.v]=true;
long j;
for (j=p[t.v]; j!=-1; j=e[j].next)//加下一堆边
{
if (!vist[e[j].v])
{
node temp;
temp.v=e[j].v;
temp.cost=e[j].cost;
q.push(temp);
}
}
}
printf("%ld\n",cost);
}
int main()
{
while (scanf("%ld%ld",&m,&n)!=EOF)
{
Prime();
}
return 0;
}
相关文章推荐
- 项目构建工具Gradle的使用入门(参考,只表明地址)
- SimpleAdapter
- Ubuntu SVN服务器的搭建与配置(转)
- 序列化.to_sym
- hadoop-test-1.2.1.jar 的一个问题
- jasperreport 文本太多,显示不全,解决办法
- 搜素算法——二分搜索-1003
- IOS 面试题总结2 核心动画
- 互联网协议入门2
- fastcgi与cgi的区别
- 压测2.0:云压测 + APM = 端到端压测解决方案
- ajax的data传参的两种方式
- jQuery DatePicker
- LeetCode:Sort List
- centos6.7 安装端口映射工具 rinetd
- C++/C源码分析
- git pull --rebase
- shell脚本的基本使用--bash
- All firmware and drivers for HP ESX servers
- 匿名管道的四种特殊情况