hdu_1102_prime
来源:互联网 发布:360怎么禁止软件联网 编辑:程序博客网 时间:2024/06/01 23:57
Constructing Roads
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.
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
mean
第一行给你n
接下来有n*n的矩阵 代表i,j间的距离
接下来输入q
接下来有q组输入,输入 x ,y 代表x,y之间已经建好路;
问建最小路的长度
#include<bits/stdc++.h>using namespace std;#define ll long longconst ll N=(ll)1e6*2+10;const ll inf=(ll)1e18;ll num[N]= {0,0,1,1},mp[6666][6666],vis[N],d[N],n;ll prime(ll s){ ll i,j,ans=0; for(i=1; i<=n; i++) { d[i]=mp[s][i]; vis[i]=0; } d[s]=0,vis[s]=1; for(i=1; i<n; i++) { ll mm=inf,k=0; for(j=1; j<=n; j++) { if(!vis[j]&&d[j]<mm) { mm=d[j]; k=j; } } // if(mm==inf) return -1; ans+=mm; vis[k]=1; for(j=1; j<=n; j++) { if(!vis[j]&&mp[k][j]<d[j]) d[j]=mp[k][j]; } } return ans;}int main(){ ll t,i,j,v[666]; while(cin>>n) { for(i=0;i<=n;i++) for(j=0;j<=n;j++) mp[i][j]=inf; for(i=1; i<=n; i++) for(j=1; j<=n; j++) { ll nu; scanf("%lld",&nu); mp[i][j]=mp[j][i]=min(mp[i][j],nu); } ll q; cin>>q; while(q--) { ll x,y; scanf("%lld%lld",&x,&y); mp[x][y]=mp[y][x]=0; } ll ans=prime(1); cout<<ans<<endl; } return 0;}
- hdu_1102_prime
- Eclipse 项目导入 Android Studio,依赖项目不让 copy 到 Android Studio 项目
- 二叉树的遍历
- 简易实现后缀表达式
- 快手2017校园招聘_笔试_面试
- 实现checkBox readonly功能的两种方式
- hdu_1102_prime
- flume+kafka+storm
- Java技术——你真的了解String类的intern()方法吗
- mysql数据库
- 最简单的vue上传图片
- hashMap的负载因子的大小与查询效率和内存空间的比例关系。
- UITableView中刷新单行cell与单个section段的方法
- Hive编程(五)【HiveQL:数据操作】
- 绘画五角星