/*Agri-Net
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 29678 Accepted: 11767
Description
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Input
The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.
Output
For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28
Source
USACO 102
*/
#include<iostream>
using namespace std;
#define INF 6000000
#define MaxN 120
int n;
int Edge[MaxN][MaxN];
int lowcost[MaxN];
int nearvex[MaxN];
void prim(int u0)
{
int i,j;
int sumweight=0;
for(i=1;i<=n;i++)
{
lowcost[i]=Edge[u0][i];
nearvex[i]=u0;
}
nearvex[u0]=-1;
for(i=1;i<n;i++)
{
int min=INF;
int v=-1;
for(j=1;j<=n;j++)
{
if(nearvex[j]!=-1&&lowcost[j]<min)
{
v=j;
min=lowcost[j];
}
}
if(v!=-1)
{
nearvex[v]=-1;
sumweight+=lowcost[v];
for(j=1;j<=n;j++)
{
if(nearvex[j]!=-1&&Edge[v][j]<lowcost[j])
{
lowcost[j]=Edge[v][j];
nearvex[j]=v;
}
}
}
}
printf("%d\n",sumweight);
}
int main()
{
int i,j,w;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
Edge[i][j]=INF;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&w);
if(i==j)
Edge[i][j]=0;
else if(w<Edge[i][j])
Edge[j][i]=Edge[i][j]=w;
}
}
prim(1);
}
return 0;
}
/*
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
28
^Z
Press any key to continue
*/
#include<iostream>
using namespace std;
#define infinity INT_MAX
#define max_vertex_num 20
typedef enum{DG,DN,UDG,UDN}GraphKind;
typedef struct ArcNode
{
int adjvex;
struct ArcNode *nextarc;
int *info;
}ArcNode;
typedef struct VNode
{
char data;
ArcNode *firstarc;
}VNode,AdjList[max_vertex_num];
typedef struct
{
AdjList vertices;
int vexnum,arcnum;
int kind;
}ALGraph;
typedef struct ArcCell
{
int adj;
//char * info;
}ArcCell,AdjMatrix[max_vertex_num][max_vertex_num];
typedef struct
{
char vexs[max_vertex_num];
AdjMatrix arcs;
int vexnum,arcnum;
GraphKind kind;
}MGraph;
int LocateVex(MGraph &G,char v)
{
int j=-1,k;
for(k=0;k<G.vexnum;k++)
if(G.vexs[k]==v)
{
j=k;
break;
}
return (j);
}
int CreateUDN(MGraph &G)
{
scanf("%d%d",&G.vexnum,&G.arcnum);
for(int i=0;i<G.vexnum;++i)
cin>>G.vexs[i];
for(i=0;i<G.vexnum;++i)
for(int j=0;j<G.vexnum;++j)
G.arcs[i][j].adj=infinity;
for(int k=0;k<G.arcnum;++k)
{
char v1,v2;
int w;
cin>>v1>>v2>>w;
i=LocateVex(G,v1);
int j=LocateVex(G,v2);
G.arcs[i][j].adj= w;
G.arcs[j][i]=G.arcs[i][j];
}
return 1;
}
int CreateGraph(MGraph &G)
{
scanf("%d",&G.kind);
switch(G.kind)
{
//case DG:return CreateDG(G);
// case DN:return CreateDN(G);
// case UDG:return CreateUDG(G);
case UDN:return CreateUDN(G);
default:return -2;
}
}
void print_Graph(MGraph &G)
{
for(int i=0;i<G.vexnum;i++)
{
for(int j=0;j<G.vexnum;j++)
if(G.arcs[i][j].adj==infinity)
printf("%4d",0);
else
printf("%4d",G.arcs[i][j].adj);
printf("\n");
}
printf("\n");
}
#include<iostream>
#include<algorithm>
#include"graph.h"
using namespace std;
struct
{
char adjvex;
int lowcost;
}closedge[max_vertex_num];
void MiniSpanTree_Prim(MGraph &G,char u)
{
int k=LocateVex(G,u);
for(int j=0;j<G.vexnum;++j)
if(j!=k)
{
closedge[j].adjvex=u;
closedge[j].lowcost=G.arcs[k][j].adj;
//cout<<closedge[j].lowcost<<endl;
}
closedge[k].lowcost=0;
for(int i=1;i<G.vexnum;++i)
{
int min =1000000;
int k0;
for(int tem=0;tem<G.vexnum;++tem)
if(closedge[tem].lowcost!=0&&closedge[tem].lowcost<min)
{
min=closedge[tem].lowcost;
k0=tem;
}
char u0=closedge[k0].adjvex;
char v0=G.vexs[k0];
printf("%4d",G.arcs[LocateVex(G,u0)][LocateVex
(G,v0)].adj);
closedge[k0].lowcost=0;
for(j=0;j<G.vexnum;++j)
if(G.arcs[k0][j].adj<closedge[j].lowcost)
{
closedge[j].lowcost=G.arcs[k0][j].adj;
closedge[j].adjvex=v0;
}
}
printf("\n");
}
int main()
{
char u;
cin>>u;
MGraph G;
CreateGraph(G);
print_Graph(G);
MiniSpanTree_Prim(G,u);
return 0;
}
/*
B
3
6 10
A B C D E F
A B 6
A D 5
A C 1
B C 5
C D 5
B E 3
E F 6
D F 2
C F 4
C E 6
*/
#include<iostream>
using namespace std;
#define INF 1000000
#define MaxN 21
int n,m;
int Edge[MaxN][MaxN];
int lowcost[MaxN];
int nearvex[MaxN];
void prim(int u0)
{
int i,j;
int sumweight=0;
for(i=1;i<=n;i++)
{
lowcost[i]=Edge[u0][i];
nearvex[i]=u0;
}
nearvex[u0]=-1;
for(i=1;i<n;i++)
{
int min=INF;
int v=-1;
for(j=1;j<=n;j++)
{
if(nearvex[j]!=-1&&lowcost[j]<min)
{
v=j;
min=lowcost[j];
}
}
if(v!=-1)
{
printf("%d %d %d\n",nearvex[v],v,lowcost[v]);
nearvex[v]=-1;
sumweight+=lowcost[v];
for(j=1;j<=n;j++)
{
if(nearvex[j]!=-1&&Edge[v][j]<lowcost[j])
{
lowcost[j]=Edge[v][j];
nearvex[j]=v;
}
}
}
}
printf("weight of MST is %d\n",sumweight);
}
int main()
{
int i,j;
int u,v,w;
scanf("%d%d",&n,&m);
memset(Edge,0,sizeof(Edge));
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
Edge[u][v]=Edge[v][u]=w;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==j)
Edge[i][j]=0;
else if(Edge[i][j]==0)
Edge[i][j]=INF;
}
}
printf("\n");
prim(1);
return 0;
}
/*
7 9
1 2 28
1 6 10
2 3 16
2 7 14
3 4 12
4 5 22
4 7 18
5 6 25
5 7 24
*/