Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10358 Accepted Submission(s): 3854
Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village 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.
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.
Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village 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.
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.
Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
Sample Input
3 0 990 692 990 0 179 692 179 0 1 1 2
Sample Output
179
Source
Recommend
Eddy
1, Prim:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=110; const int INF=0x3f3f3f3f; int n,ans; int map[maxn][maxn],dis[maxn],vis[maxn]; void Prim(){ int i; for(i=1;i<=n;i++){ dis[i]=map[1][i]; vis[i]=0; } dis[1]=0; vis[1]=1; int j,k,tmp; for(i=1;i<=n;i++){ tmp=INF; for(j=1;j<=n;j++) if(!vis[j] && tmp>dis[j]){ k=j; tmp=dis[j]; } if(tmp==INF) return ; ans+=dis[k]; vis[k]=1; for(j=1;j<=n;j++) if(!vis[j] && dis[j]>map[k][j]) dis[j]=map[k][j]; } } int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d",&n)){ for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&map[i][j]); int q,u,v; scanf("%d",&q); while(q--){ scanf("%d%d",&u,&v); map[u][v]=map[v][u]=0; } ans=0; Prim(); printf("%d\n",ans); } return 0; }
2, Kruskal():
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=110; struct Edge{ int u,v; int w; }edge[maxn*100]; int n,ans,cnt; int father[maxn],rank[maxn]; void addedge(int cu,int cv,int cw){ edge[cnt].u=cu; edge[cnt].v=cv; edge[cnt].w=cw; cnt++; } void makeSet(){ for(int i=1;i<=n;i++){ father[i]=i; rank[i]=1; } } int findSet(int x){ if(x!=father[x]){ father[x]=findSet(father[x]); } return father[x]; } int cmp(Edge a,Edge b){ return a.w<b.w; } void Kruskal(){ sort(edge,edge+cnt,cmp); for(int i=0;i<cnt;i++){ int fx=findSet(edge[i].u); int fy=findSet(edge[i].v); if(fx==fy) continue; ans+=edge[i].w; if(rank[fx]>=rank[fy]){ father[fy]=fx; rank[fx]+=rank[fy]; }else{ father[fx]=fy; rank[fy]+=rank[fx]; } } } int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d",&n)){ cnt=0; makeSet(); int w; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ scanf("%d",&w); addedge(i,j,w); } int q,u,v; scanf("%d",&q); while(q--){ scanf("%d%d",&u,&v); int fx=findSet(u); int fy=findSet(v); if(rank[fx]>=rank[fy]){ father[fy]=fx; rank[fx]+=rank[fy]; }else{ father[fx]=fy; rank[fy]+=rank[fx]; } } ans=0; Kruskal(); printf("%d\n",ans); } return 0; }
相关推荐
HDUACM201509版_07并查集(最小生成树).ppt文件很可能包含了关于这个问题的详细讲解,包括并查集的建立、维护以及如何与Kruskal算法结合来求解最小生成树的问题。 并查集是一种用于处理连接关系的数据结构,它可以...
杭电ACM课件2014版之 (HDUACM201403版_06)并查集(最小生成树)
(HDUACM2010版_06)并查集(最小生成树(HDUACM2010版_06)并查集(最小生成树(HDUACM2010版_06)并查集(最小生成树(HDUACM2010版_06)并查集(最小生成树
4. **图论算法**:最短路径算法(Dijkstra、Floyd-Warshall)、拓扑排序、最小生成树(Prim、Kruskal)等。 5. **数学方法**:模运算、数论、组合数学等。 6. **字符串处理**:KMP匹配、Z算法、后缀数组、AC自动机等...
2. **图论与网络流**:可能涉及最小生成树(Prim或Kruskal算法)、最短路径(Dijkstra或Floyd-Warshall算法)、网络流(Ford-Fulkerson或Edmonds-Karp算法)等,这些都是解决复杂问题的利器,常见于竞赛编程和实际...
5. **图论**:图的遍历(深度优先搜索DFS和广度优先搜索BFS)、最小生成树(Prim和Kruskal算法)、最短路径(Dijkstra和Floyd算法)等。在解决网络问题、旅行商问题等时,图论知识至关重要。 6. **递推和动态规划**...
1. **基础算法**:这是ACM编程的基础,包括排序(快速排序、归并排序、堆排序等)、搜索(二分查找、深度优先搜索、广度优先搜索)、图论(最短路径算法如Dijkstra和Floyd-Warshall,最小生成树如Prim和Kruskal)等...
3. **图论算法**:深度优先搜索(DFS)、广度优先搜索(BFS)、最小生成树(Prim或Kruskal算法)、最短路径问题(Dijkstra或Floyd算法)等,这些都是解决复杂网络问题的关键工具。 4. **动态规划**:背包问题、最长...
3. **图论**:包括最短路径算法(Dijkstra、Floyd-Warshall、Bellman-Ford)、最小生成树(Prim、Kruskal)、拓扑排序等。 4. **字符串处理**:如KMP算法、Manacher's Algorithm、后缀数组、Z-Algorithm等,常用于...
2. **数据结构**:如链表、树、堆、栈、队列、图等,以及它们在解决问题中的应用,比如最小生成树(Prim或Kruskal)、最短路径(Bellman-Ford)等。 3. **动态规划**:这是一种强大的解决问题的方法,常常用于处理...
7. 图的最小生成树:图的最小生成树算法、图的 Kruskal 算法等。 * 题目1195:Open the Lock,涉及到图的最小生成树算法。 * 题目1198:Farm Irrigation,涉及到图的 Kruskal 算法概念。 8. 图的拓扑排序:图的拓扑...
经典的算法有Prim算法和Kruskal算法,它们分别基于加边和加边的原则找到最小生成树。 5. **搜索**:搜索算法在ACM中至关重要,包括深度优先搜索(DFS)和广度优先搜索(BFS)。它们用于遍历图或树结构,解决路径...
常见的解决最小生成树问题的算法有Prim算法和Kruskal算法。在"畅通工程"的问题描述中,可能需要找到一种方式,使得在给定的网络中,连接所有节点的边的总权重最小,以优化交通或通信网络的效率。 Prim算法是一种...
4. **图论**:包括深度优先搜索(DFS)、广度优先搜索(BFS)、最短路径算法(Dijkstra、Floyd-Warshall)、最小生成树(Prim、Kruskal)等。2101-2150号题目可能会涉及图的建立和分析,如网络流问题、旅行商问题等...
3. **图论**:如果涉及到母牛的移动路径或者关系网络,可能需要用到图论中的概念,如最短路径算法(Dijkstra's算法或Floyd-Warshall算法)、最小生成树(Prim或Kruskal算法)等。 4. **回溯法**:对于组合优化问题...
10. **图论问题**:包括最小生成树(Prim算法和Kruskal算法)、最短路径(Dijkstra算法和Bellman-Ford算法)、网络流(Ford-Fulkerson算法和Edmonds-Karp算法)等。 通过深入学习和实践这些解题报告,不仅可以提升...
2. **图论算法**:包括最短路径问题(Dijkstra算法、Floyd算法)、最小生成树(Prim算法、Kruskal算法)等。解题报告会解析图结构,展示如何构建和遍历图,以找到最优解决方案。 3. **贪心算法**:贪心算法在每一步...
- **算法应用**:最小生成树算法如Prim或Kruskal,用于构建连接所有节点的最低成本路径。 #### 4. MinimumTransportCost (最低运输成本) - **问题描述**:涉及物流规划,寻找运输物品的最低成本路线。 - **算法应用...
3. **数据结构**:数组、链表、栈、队列、树(二叉树、平衡树如AVL和红黑树)、图(邻接矩阵、邻接表、最小生成树、拓扑排序等)。 4. **字符串处理**:KMP算法、Manacher's Algorithm、Rabin-Karp算法等。 5. **...
图的概念和术语、存储结构(数组表示和邻接表)、遍历(深度优先搜索和广度优先搜索)以及连通性问题(如最小生成树算法Prim和Kruskal)是图论的基础。 查找部分,二分查找是有序表中高效的查找方法,而哈希表则...