Ice_cream’s world III
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
Description
ice_cream’s world becomes stronger and stronger; every road is built as undirected. The queen enjoys traveling around her world; the queen’s requirement is like II problem, beautifies the roads, by which there are some ways from every city to the capital. The project’s cost should be as less as better.
Input
Every case have two integers N and M (N<=1000, M<=10000) meaning N cities and M roads, the cities numbered 0…N-1, following N lines, each line contain three integers S, T and C, meaning S connected with T have a road will cost C.
Output
If Wiskey can’t satisfy the queen’s requirement, you must be output “impossible”, otherwise, print the minimum cost in this project. After every case print one blank.
Sample Input
2 1
0 1 10
4 0
Sample Output
10
impossible
记得输出两个空行
//普里姆-Prim
#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<stack>
#include<set>
#include<bitset>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=1005;
const int MAXM=10005;
int N,M;
int Pic[MAXN][MAXN],Dis[MAXN];
bool Visited[MAXN];
int main()
{
int a,b,c;
while (scanf("%d%d",&N,&M)!=EOF)
{
memset(Pic,INF,sizeof(Pic));
memset(Visited,false,sizeof(Visited));
for (int i=0; i<M; i++)
{
scanf("%d%d%d",&a,&b,&c);
Pic[a][b]=min(Pic[a][b],c);
Pic[b][a]=min(Pic[b][a],c);
}
int Ans=0,u;
Visited[0]=true;
for (int i=0; i<N; i++)
Dis[i]=Pic[0][i];
for (int i=1; i<N; i++)
{
int minlen=INF;
for (int i=0; i<N; i++)
if (!Visited[i]&&Dis[i]<minlen)
minlen=Dis[i],u=i;
Visited[u]=true;
Ans+=minlen;
for (int i=0; i<N; i++)
if (!Visited[i]&&Dis[i]>Pic[u][i])
Dis[i]=Pic[u][i];
}
bool flag=true;
for (int i=0;i<N;i++)
if (!Visited[i])
{
flag=false;
break;
}
if (flag)
printf("%d\n\n",Ans);
else
printf("impossible\n\n");
}
return 0;
}
//克鲁斯卡尔-Kruskal
#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<stack>
#include<set>
#include<bitset>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXM=10005;
struct Edge
{
int u,v,x;
bool operator < (const Edge& a) const
{
return x<a.x;
}
};
Edge Es[MAXM];
int Father[MAXM];
int N,M,Cnt;
int Find(int x)
{
int r=x;
while (Father[r]!=r)
r=Father[r];
int i=x,j;
while (Father[i]!=r)
{
j=Father[i];
Father[i]=r;
i=j;
}
return r;
}
bool Union(int x,int y)
{
int fx=Find(x),fy=Find(y);
if (fx==fy)
return false;
Father[fy]=fx;
return true;
}
int main()
{
while (scanf("%d%d",&N,&M)!=EOF)
{
int Ans=0,x=0;
for (int i=0; i<M; i++)
scanf("%d%d%d",&Es[i].u,&Es[i].v,&Es[i].x);
sort(Es,Es+M);
for (int i=0; i<N; i++)
Father[i]=i;
for (int i=0; i<M; i++)
if (Union(Es[i].u,Es[i].v))
{
Ans+=Es[i].x,x++;
if (x==N-1)
break;
}
if (x==N-1)
printf("%d\n\n",Ans);
else
printf("impossible\n\n");
}
return 0;
}