1.prim
//使用邻接表存储,prim求最小生成树的权值之和
#include<stdio.h>
#include<iostream>
#include<vector>
using namespace std;
const int maxn=1000;
const int INF=100000;
int n;
int d[maxn];//每个点与集合之间的距离
int vis[maxn]={0};
struct node{
int v,dis;
};
vector<node> Adj[maxn];
int prim(int s){
int i,j,v,u,min;
int ans=0;
fill(d,d+maxn,INF);
d[s]=0;
for(i=0;i<n;i++){
u=-1;
min=INF;
for(j=0;j<n;j++){
if(d[j]<INF&&vis[j]==0){
u=j;
min=d[j];
}
}
if(u==-1) return -1;
vis[u]=1;
ans+=d[u];
for(j=0;j<Adj[u].size();j++){
v=Adj[u][j].v;
if(vis[v]==0&&Adj[u][j].dis<d[v]){
d[v]=Adj[u][j].dis;
}
}
}
return ans;
}
int main(){
int m,i,j,a,b,c;
cin>>n>>m;
for(i=0;i<m;i++){
cin>>a>>b>>c;
node *t=new node;
t->dis=c;t->v=b;
Adj[a].push_back(*t);
node *p=new node;
p->dis=c;p->v=a;
Adj[b].push_back(*p);
}
int ans=prim(0);
cout<<ans<<endl;
return 0;
}
/*6 10
0 1 4
0 4 1
0 5 2
1 2 6
1 5 3
2 3 6
2 5 5
3 4 4
3 5 5
4 5 3
<<15*/
2.kruskal
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1000;
struct edge{
int u,v;
int cost;
}E[maxn];
int father[maxn];
int n,m;
bool cmp(edge a,edge b){
return a.cost<b.cost;
}
int findfather(int a){
int x=a;
while(a!=father[a]){
a=father[a];
}
while(x!=father[x]){
int z=x;
x=father[x];
father[z]=a;
}
return a;
}
void initial(){
int i;
for(i=0;i<n;i++){
father[i]=i;
}
}
int kruskal(){
int i;
int ans=0;
int edge_num=0;
initial();
sort(E,E+m,cmp);
for(i=0;i<m;i++){
int f1=findfather(E[i].u);
int f2=findfather(E[i].v);
if(f1!=f2){
father[f1]=f2;//father[E[i].u]=f2!!!???
ans+=E[i].cost;
edge_num++;
if(edge_num==n-1) return ans;
}
}
if(edge_num<n-1) return -1;
else return ans;
}
int main(){
cin>>n>>m;
int i;
for(i=0;i<m;i++){
int a,b,c;
cin>>a>>b>>c;
E[i].u=a;
E[i].v=b;
E[i].cost=c;
}
int ans=kruskal();
cout<<ans;
return 0;
}
/*6 10
0 1 4
0 4 1
0 5 2
1 2 1
1 5 3
2 3 6
2 5 5
3 4 5
3 5 4
4 5 3
<<11*/
问题 C: 畅通工程
时间限制: 1 Sec 内存限制: 32 MB提交: 25 解决: 23
[ 提交][ 状态][ TK题库][命题人: ]
题目描述
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。
输入
测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M (N, M < =100 );随后的 N 行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。
输出
对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。
样例输入
3 4
1 2 1
2 3 2
3 4 3
2 4
1 2 1
3 4 2
0 5
样例输出
6
?
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=5050;
int father[maxn];
struct edge{
int u,v;
int cost;
int flag;
};
bool cmp(edge a,edge b){
if(a.flag==1&&b.flag==1) return a.cost<b.cost;
else if(a.flag==1&&b.flag==0) return true;
else if(a.flag==0&&b.flag==1) return false;
else return a.cost<b.cost;
}
void initial(int n){
int i;
for(i=1;i<=n;i++){
father[i]=i;
}
}
int findfather(int a){
int x=a;
while(a!=father[a]){
a=father[a];
}
while(x!=father[x]){
int z=x;
x=father[x];
father[z]=a;
}
return a;
}
int kruskal(int n,int m,edge E[]){
int i,j,ans=0;
int num_edge=0;
initial(n);
sort(E,E+m,cmp);
for(i=0;i<m;i++){
int f1=findfather(E[i].u);
int f2=findfather(E[i].v);
if(f1!=f2){
father[f1]=f2;
if(E[i].flag==1) num_edge++;
else{
ans+=E[i].cost;
num_edge++;
}
if(num_edge==n-1) return ans;
}
}
if(num_edge<n-1) return -1;
else return ans;
}
int main(){
int n,m;
cin>>n;
while(n!=0){
int i;
m=n*(n-1)/2;
edge E[maxn];
for(i=0;i<m;i++){
int a,b,c,d;
cin>>a>>b>>c>>d;
E[i].u=a;
E[i].v=b;
E[i].cost=c;
E[i].flag=d;
}
int ans=kruskal(n,m,E);
if(ans==-1) cout<<"?"<<endl;
else cout<<ans<<endl;
cin>>n;
}
return 0;
}
3.floyd
#include<stdio.h>
#include<iostream>
using namespace std;
const int maxn=100;
const int INF=10000;
int d[maxn][maxn];
int n;
void floyd(){
int i,j,k;
for(k=0;k<n;k++){
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(d[i][k]!=INF&&d[k][j]!=INF&&d[i][k]+d[k][j]<d[i][j]){
d[i][j]=d[i][k]+d[k][j];
printf("d[%d][%d]=%d\n",i,j,d[i][j]);
}
}
}
}
}
int main(){
int m,i,j;
cin>>n>>m;
fill(d[0],d[0]+maxn*maxn,INF);
for(i=0;i<n;i++){
d[i][i]=0;
}
for(i=0;i<m;i++){
int a,b,c;
cin>>a>>b>>c;
d[a][b]=c;
}
floyd();
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%d ",d[i][j]);
}
printf("\n");
}
return 0;
}