NSOJ Constructing Roads(图论)
2016-03-22 18:33
155 查看
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.
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.
最小生成树....先建图,之后再处理相同地点....只需要遍历一半就行....
时间超限:
View Code
AC代码:
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.
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
最小生成树....先建图,之后再处理相同地点....只需要遍历一半就行....
时间超限:
#include <vector> #include <map> #include <set> #include <algorithm> #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <string> #include <cstring> #include <queue> using namespace std; #define INF 0x3f3f3f3f #define MAX 1000000 int n,ans; int dis[111],vis[111],mp[111][111]; void prim() { memset(vis,0,sizeof(vis)); memset(dis,INF,sizeof(dis)); dis[1]=0;ans=0;dis[0]=INF; while(true){ int m=0; for(int i=1; i<=n; i++){ if(!vis[i] && (dis[i]<dis[m])) m=i; } if(m==0) break; vis[m]=1; ans+=dis[m]; for(int i=1; i<=n; i++) dis[i]=min(dis[i],mp[m][i]); } } int main() { int x,a,b; while(scanf("%d",&n)){ if(n==1) break; for(int i=1; i<=n; i++){ for(int j=1; j<=n; j++){ scanf("%d",&mp[i][j]); } } scanf("%d",&x); while(x--){ scanf("%d%d",&a,&b); mp[a][b]=mp[b][a]=0; } prim(); printf("%d\n",ans); } }
View Code
AC代码:
#include <vector> #include <map> #include <set> #include <algorithm> #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <string> #include <cstring> #include <queue> using namespace std; #define INF 0x3f3f3f3f #define MAX 1000000 int n,ans; int dis[111],vis[111],mp[111][111]; void prim() { memset(vis,0,sizeof(vis)); memset(dis,INF,sizeof(dis)); dis[1]=0; ans=0; dis[0]=INF; while(true){ int m=0; for(int i=1; i<=n; i++){ if(!vis[i] && dis[i]<dis[m]) m=i; } if(m==0) break; vis[m]=1; ans+=dis[m]; for(int i=1; i<=n; i++) dis[i]=min(dis[i],mp[m][i]); } } int main() { int x,a,b; while(scanf("%d",&n)==1){ for(int i=1; i<=n; i++){ for(int j=1; j<=n; j++){ scanf("%d",&mp[i][j]); } } scanf("%d",&x); while(x--){ scanf("%d%d",&a,&b); mp[a][b]=mp[b][a]=0; } prim(); printf("%d\n",ans); } }
相关文章推荐
- IOS沙盒机制
- KMP 题解
- iBatis中关于count(*)的写法
- Oracle Study之-AIX6.1构建Oracle 10gR2 RAC(1)
- 基于正点原子战舰开发板的贪吃蛇游戏
- 5分钟弄懂Docker!
- Oracle 学习笔记 11 -- 视图 (VIEW)
- 实习笔试题,源代码编译
- java中queue的使用
- 树莓派日记之 GPIO
- 《练手_快排的实现》
- Less语言特性
- webservice SOAP WSDL UDDI
- Spark—聚合操作—combineByKey
- c:forEach的varStatus属性
- 百分点技术负责人:我们为什么需要大数据操作系统
- 使用node的http模块实现爬虫功能,并把爬到的数据存入mongondb
- NSOJ 畅通工程(并查集)
- LeetCode 15 3Sum(尺取法或哈希)
- Java贪心算法: 田忌赛马