HDU 1102 Constructing Roads, Prim+优先队列
题目链接:HDU 1102 Constructing Roads
Constructing Roads
C such that there is a road between A and C, and C and B are connected.
We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
i and village j.
Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
3
0 990 692
990 0 179
692 179 0
1
1 2
179
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std; #define maxn 110
#define INF 0xffff
int g[maxn][maxn];
int n; struct node
{
int v, key;
friend bool operator<(node a, node b)
{
return a.key > b.key;
}
}; bool visited[maxn];
node vx[maxn];
priority_queue<node> q;
void Prim()
{
for(int i = 1; i <= n; i++)
{
vx[i].v = i;
vx[i].key = INF;
visited[i] = false;
}
vx[1].key = 0;
q.push(vx[1]);
while(!q.empty())
{
node nd = q.top();
q.pop();
int st = nd.v;
if(visited[st])
continue;
visited[st] = true;
for(int j = 1; j <= n; j++)
{
if(j != st && !visited[j] && vx[j].key > g[st][j])
{
vx[j].key = g[st][j];
q.push(vx[j]);
}
}
}
}
int main()
{
int m, a, b;
while(~scanf("%d", &n))
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
scanf("%d", &g[i][j]);
g[i][i] = INF;
}
scanf("%d", &m);
while(m--)
{
scanf("%d%d", &a, &b);
g[a][b] = g[b][a] = 0;
}
Prim();
int ans = 0;
for(int i = 1; i <= n; i++)
ans += vx[i].key;
printf("%d\n", ans); }
return 0;
}
邻接表:
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std; #define maxn 110 //最大顶点个数
int n, m; //顶点数,边数 struct arcnode //边结点
{
int vertex; //与表头结点相邻的顶点编号
int weight; //连接两顶点的边的权值
arcnode * next; //指向下一相邻接点
arcnode() {}
arcnode(int v,int w):vertex(v),weight(w),next(NULL) {}
}; struct vernode //顶点结点,为每一条邻接表的表头结点
{
int vex; //当前定点编号
arcnode * firarc; //与该顶点相连的第一个顶点组成的边
}Ver[maxn]; void Init() //建立图的邻接表须要先初始化,建立顶点结点
{
for(int i = 1; i <= n; i++)
{
Ver[i].vex = i;
Ver[i].firarc = NULL;
}
}
void Insert(int a, int b, int w) //尾插法,插入以a为起点,b为终点,权为w的边,效率不如头插,可是能够去重边
{
arcnode * q = new arcnode(b, w);
if(Ver[a].firarc == NULL)
Ver[a].firarc = q;
else
{
arcnode * p = Ver[a].firarc;
if(p->vertex == b)
{
if(p->weight > w)
p->weight = w;
return ;
}
while(p->next != NULL)
{
if(p->next->vertex == b)
{
if(p->next->weight > w);
p->next->weight = w;
return ;
}
p = p->next;
}
p->next = q;
}
}
void Insert2(int a, int b, int w) //头插法,效率更高,但不能去重边
{
arcnode * q = new arcnode(b, w);
if(Ver[a].firarc == NULL)
Ver[a].firarc = q;
else
{
arcnode * p = Ver[a].firarc;
q->next = p;
Ver[a].firarc = q;
}
}
struct node //保存key值的结点
{
int v;
int key;
friend bool operator<(node a, node b) //自己定义优先级,key小的优先
{
return a.key > b.key;
}
}; #define INF 0xfffff //权值上限
bool visited[maxn]; //是否已经增加树
node vx[maxn]; //保存每一个结点与其父节点连接边的权值
priority_queue<node> q; //优先队列stl实现
void Prim() //s表示根结点
{
for(int i = 1; i <= n; i++) //初始化
{
vx[i].v = i;
vx[i].key = INF;
visited[i] = false;
}
vx[1].key = 0;
q.push(vx[1]);
while(!q.empty())
{
node nd = q.top(); //取队首,记得赶紧pop掉
q.pop();
if(visited[nd.v]) //注意这一句的深意,避免非常多不必要的操作
continue;
visited[nd.v] = true;
arcnode * p = Ver[nd.v].firarc;
while(p != NULL) //找到全部相邻结点,若未訪问,则入队列
{
if(!visited[p->vertex] && p->weight < vx[p->vertex].key)
{
vx[p->vertex].key = p->weight;
vx[p->vertex].v = p->vertex;
q.push(vx[p->vertex]);
}
p = p->next;
}
}
} int main()
{
int m, a, b, x;
while(~scanf("%d", &n))
{
Init();
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
scanf("%d", &x);
if(x != 0)
Insert2(i, j, x);
}
}
scanf("%d", &m);
while(m--)
{
scanf("%d%d", &a, &b);
Insert(a, b, 0);
Insert(b, a, 0);
}
Prim();
int ans = 0;
for(int i = 1; i <= n; i++)
ans += vx[i].key;
printf("%d\n", ans); }
return 0;
}
HDU 1102 Constructing Roads, Prim+优先队列的更多相关文章
- HDU 1102(Constructing Roads)(最小生成树之prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...
- hdu 1102 Constructing Roads (Prim算法)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- hdu 1102 Constructing Roads (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- hdu 1102 Constructing Roads Kruscal
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...
- HDU 1102 Constructing Roads
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1102 Constructing Roads(kruskal)
Constructing Roads There are N villages, which are numbered from 1 to N, and you should build some r ...
- hdu 1102 Constructing Roads(最小生成树 Prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...
- hdu 1102 Constructing Roads(kruskal || prim)
求最小生成树.有一点点的变化,就是有的边已经给出来了.所以,最小生成树里面必须有这些边,kruskal和prim算法都能够,prim更简单一些.有一点须要注意,用克鲁斯卡尔算法的时候须要将已经存在的边 ...
随机推荐
- csdn仍是"待定"对?
正如标题,我的博客会审查,?我们见证.如此反复.考虑到该博客平台的变化. 看来,这次最终逃脱被"待审核",看来再也不用受这个困扰了,希望以后CSDN可以在 ...
- 2014 ACM湖南匹配10会议省赛
2014湖南游戏..... 1:牡丹江Regional有些球队没来的冲突 2:题目比較水 3:队友神勇发挥 最终在开局不利的情况下完毕了翻盘,拿到了第二名.....没有抓住机会顺势夺冠还是非常遗憾的. ...
- 建立ORACLE10G DATA GUARD--->Physical Standby
下面是我自己建Physical Standby,按照下面的步骤一步我一步,当然,打造成功,以下步骤可以作为建筑物Data Guard结构操作手册. HA和DG差额:HA:可以做IP切换自己主动 DG ...
- 云梯vpn
刚和大饼合买了一个云梯的vpn 表示可以把俺的优惠连接放出来了 貌似必须是新注册用户才能够享用优惠 http://protizi.com/?r=5e3fecd7eae558ec 把云梯推荐给朋友们 让 ...
- codeforces Round #259(div2) E解决报告
E. Little Pony and Summer Sun Celebration time limit per test 1 second memory limit per test 256 meg ...
- HDU 1754 I Hate It (段树单点更新)
Problem Description 很多学校更受欢迎的习惯. 老师们真的很喜欢问.从XX XX到其中,的是多少. 这让非常多学生非常反感. 无论你喜不喜欢,如今须要你做的是,就是依照老师的要求.写 ...
- STL源代码分析--迭代摘要、迭代器失效汇总
Vector 1.内部数据结构:连续存储,比如数组. 2.随机訪问每一个元素,所须要的时间为常量. 3.在末尾添加或删除元素所需时间与元素数目无关,在中间或开头添加或删除元素所需时间随元素数目呈线性变 ...
- Oracle 中用一个表的数据更新另一个表的数据
Oracle 中用一个表的数据更新另一个表的数据 分类: SQL/PLSQL2012-05-04 15:49 4153人阅读 评论(1) 收藏 举报 oraclemergesubqueryinsert ...
- TestNg它@Factory详细解释------如何更改参数值测试
原创文章,版权所有所有.转载,归因:http://blog.csdn.net/wanghantong TestNg的@Factory注解从字面意思上来讲就是採用工厂的方法来创建測试数据并配合完毕測试 ...
- struts1吊牌<logic:iterate>
<logic:iterate>主要用于处理网页上的输出集合,集合是其中一般下列之一: 1. java对象的数组 2. ArrayList.Vector.HashMap等 具体使用方法请參考 ...