当前位置:网站首页>I - constructing roads
I - constructing roads
2022-06-30 15:00:00 【Rabbit doesn't like radish】
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
The question :
Give a number n Indicates the number of villages
Next n*n Matrix ,map[i][j] The surface from the village i To the village j Distance of
One number on the next line k
Next k That's ok , Two numbers per line a,b, Indicate Village a To the village b The road has been built ,
Find the shortest circuit length to be repaired .
I'm a Han Han , I have been thinking that he gave me the distance between the two villages , It means no more calculation ( There is nothing wrong with that ), But I am obsessed with how to ignore the given road when traversing the road , alas , After looking at other people's, I found that I could just let the road be 0, ahhhh , How simple , I'm really speechless to myself !!!!!!
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=200,MAX=0x3f3f3f3f;
int n,m;
int from,to,w;
int dist[N],map[N][N];
bool biao[N];
int prim()
{
memset(dist,0x3f,sizeof(dist));
memset(biao,0,sizeof(biao));
dist[1]=0,biao[1]=1;
int t=1,sum=0;
for(int i=0;i<n-1;i++)
{
for(int j=1;j<=n;j++)
{
dist[j]=min(dist[j],map[t][j]);
}
t=-1;
for(int j=1;j<=n;j++)
{
if(!biao[j]&&(t==-1||dist[t]>dist[j]))
t=j;
}
sum+=dist[t];
biao[t]=1;
}
return sum;
}
int main()
{
while(cin>>n)
{
memset(map,0x3f,sizeof(map));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>map[i][j];
}
}
cin>>m;
while(m--)
{
int a,b;
cin>>a>>b;
map[a][b]=map[b][a]=0;
}
int ans=prim();
cout<<ans<<endl;
}
return 0;
}
边栏推荐
- 【BUUCTF】[GXYCTF2019]Ping Ping Ping1
- CCF string matching (Full Score code + problem solving ideas + skill summary) March 3, 2014
- Matlab calculates the factorial sum of the first n numbers (easy to understand)
- Text matching - [naacl 2022] GPL
- [buuctf] [actf2020 freshman competition]include
- Clear the route cache in Vue
- 高精度CNC加工中心为什么会出现误差?这4个原因你要注意!
- Binary rotation array (1)
- Double pointer circular linked list
- 1076 forwards on Weibo (30 points)
猜你喜欢

Clear the route cache in Vue

CCF call auction (full mark code + problem solving ideas + skill summary) 201412 - 3

ES6 notes

CCF numerical sorting (Full Score code + problem solving ideas + skill summary) 201503-2

1 figure to explain the difference and connection between nodejs and JS

CCF window (Full Score code + problem solving idea) March 2, 2014

Learn about data kinship JSON format design from sqlflow JSON format

August 24, 2021 deque queue and stack

Determine the number of digits of an integer in MATLAB (one line of code)

CCF sequence segmentation (Full Score code + problem solving idea) 201509 -1
随机推荐
CCF window (Full Score code + problem solving idea) March 2, 2014
Matlab two-dimensional array example (extract data)
Clear the route cache in Vue
Searching for single element in dichotomy
数控加工中心打刀缸工作原理及故障处理
Binary rotation array (2)
The difference between queue and stack
1015 reversible primes (20 points)
An error is reported when installing dataspherestudio doc: invalid default value for 'update_ time‘
LIS error: this configuration section cannot be used in this path
Vue returns to the previous page without refreshing the page / Vue caches the page
1136: password translation
Summary of C language interview questions
V3_ Chrome extended Chinese translation document V3 directory
[extensive reading of papers] a delicious recipe analysis framework for exploring multi modal recipes with variable attributes
Matlab draws the image of the larger function value of the two functions (super simple)
Double pointer letter matching
1066 root of AVL tree (25 points)
文本匹配——【NAACL 2022】GPL
DR-TANet: Dynamic Receptive Temporal Attention Network for Street Scene Change Detection