2013
11-27

# HDU 1102 Constructing Roads-最小生成树-[解题报告] C++

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.

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.

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.

3
0 990 692
990 0 179
692 179 0
1
1 2

179

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;

/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/

int n,m;//n个点，总共有m跳边，单向边
int xh[105][105];
int parent[105];//父亲节点
struct se
{
int x,y,w;
}edge[5055];

bool emp(se a,se b)
{
return a.w<b.w;
}

void UFset()
{
for(int i=0;i<=n;i++)
parent[i]=i;
}

int find(int x)
{
int r=x;
while(x!=parent[x])
x=parent[x];
while(r!=x)//剪枝
{
int j=parent[r];
parent[r]=x;
r=j;
}
return x;
}

void Kruskal()
{
int sum=0,i,count=0;
UFset();
for(i=0;i<m;i++)
{
int x=edge[i].x;
int y=edge[i].y;
int fx=find(x),fy=find(y);
if(fx!=fy)
{
count++;
parent[fx]=fy;
sum+=edge[i].w;
if(count==n-1)
break;
}
}
cout<<sum<<endl;
}

int main()
{
int i,j,k,x;
while(cin>>n)
{
m=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&x);
if(i<j)//因为是单向的，所以只用i<j右上角那一边就行了
{
edge[m].x=i;
edge[m].y=j;
edge[m].w=x;
xh[i][j]=m;//记录i和j是第几条边
m++;
}
}
cin>>k;
while(k--)
{
int a,b;
scanf("%d%d",&a,&b);
if(a>b)//和上面一样，始终保持edge[i][j],i<j
swap(a,b);
edge[xh[a][b]].w=0;//已经有路了，w就记为0
}
sort(edge,edge+m,emp);//边从小到大排序
Kruskal();
}
return 520;
}

1. I like your publish. It is great to see you verbalize from the coronary heart and clarity on this essential subject matter can be easily noticed.

3. 这道题目的核心一句话是：取还是不取。
如果当前取，则index+1作为参数。如果当前不取，则任用index作为参数。

4. /*
* =====================================================================================
*
* Filename: 1366.cc
*
* Description:
*
* Version: 1.0
* Created: 2014年01月06日 14时52分14秒
* Revision: none
* Compiler: gcc
*
* Author: Wenxian Ni (Hello World~), [email protected]
* Organization: AMS/ICT
*
* =====================================================================================
*/

#include
#include

using namespace std;

int main()
{
stack st;
int n,i,j;
int test;
int a[100001];
int b[100001];
while(cin>>n)
{
for(i=1;i>a[i];
for(i=1;i>b[i];
//st.clear();
while(!st.empty())
st.pop();
i = 1;
j = 1;

while(in)
break;
}
while(!st.empty()&&st.top()==b[j])
{
st.pop();
j++;
}
}
if(st.empty())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}