1000
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
#define M 102
#define INF 1<<28
int graph[M][M];
int dist[M];
int n,m;//n 道路数
void prim()
{
int j,k;
int vis[M];
for(int i=0;i<M;i++)
dist[i]=INF;
memset(vis,0,sizeof(vis));
dist[1]=0;
for(int i=1;i<=m;i++)
{
k=-1;
for(j=1;j<=m;j++)
if(!vis[j]&&(k==-1||dist[j]<dist[k]))
{
k=j;
}
vis[k]=1;
for(j=1;j<=m;j++)//更新已选点到未选点集的最短距离
if(!vis[j]&&graph[k][j]>0&&graph[k][j]<dist[j])
{
dist[j]=graph[k][j];
}
}
int ans=0;
for(int i=1;i<=m;i++)
ans+=dist[i];
if(ans>INF)
printf("?\n");
else
printf("%d\n",ans);
}
int main()
{
int x,y,cost;
while(scanf("%d%d",&n,&m)==2)
{
if(n==0)break;
memset(graph,0,sizeof(graph));
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&x,&y,&cost);
if(graph[x][y]==0||graph[x][y]>cost)
graph[x][y]=graph[y][x]=cost;
}
prim();
}
return 0;
}
1001
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
#define M 1002
#define INF 1<<28
int x,y;
int graph[M][M];
int vis[M],link[M];
bool dfs(int idx)
{
for(int j=1;j<=y;j++)
if(!vis[j]&&graph[idx][j])
{
vis[j]=1;
if(link[j]==-1||dfs(link[j]))
{
link[j]=idx;
return 1;
}
}
return 0;
}
int main()
{
int k;
int a,b;
while(~scanf("%d",&k))
{
if(k==0)break;
scanf("%d%d",&x,&y);
//1.注意数组要清零
memset(graph,0,sizeof(graph));
for(int i=0;i<k;i++)
{
scanf("%d%d",&a,&b);
graph[a][b]=1;
}
int cnt=0;
memset(link,-1,sizeof(link));
for(int i=1;i<=x;i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i))
cnt++;
}
printf("%d\n",cnt);
}
return 0;
}
1002
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;
#define M 102
#define INF 1<<28
double graph[M][M];
double dist[M];
int m;
struct point {
int x,y;
}p[M];
void prim()
{
int j,k;
int vis[M];
for(int i=0;i<M;i++)
dist[i]=INF;
memset(vis,0,sizeof(vis));
dist[1]=0;
for(int i=1;i<=m;i++)
{
k=-1;
for(j=1;j<=m;j++)
if(!vis[j]&&(k==-1||dist[j]<dist[k]))
{
k=j;
}
vis[k]=1;
for(j=1;j<=m;j++)//更新已选点到未选点集的最短距离
if(!vis[j]&&graph[k][j]>0&&graph[k][j]<dist[j])
{
dist[j]=graph[k][j];
}
}
double ans=0;
for(int i=1;i<=m;i++)
ans+=dist[i];
if(ans>INF)
printf("oh!\n");
else
printf("%.1lf\n",ans*100);
}
double getdistance(point p1,point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
int main()
{
int k;
scanf("%d",&k);
while(k--)
{
scanf("%d",&m);
memset(graph,-1,sizeof(graph));
for(int i=1;i<=m;i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
}
for(int i=1;i<=m;i++)
for(int j=i+1;j<=m;j++)
{
double dis=getdistance(p[i],p[j]);
if(dis<=1000.0&&dis>=10.0)
graph[i][j]=graph[j][i]=dis;
}
prim();
}
return 0;
}
1003
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;
#define M 502
#define INF 1<<28
int graph[M][M];
int indegree[M];
int n;
int ans[M],index;
void topsort()
{
int k;
for(int i=1;i<=n;i++)
{
for(k=1;k<=n;k++)
if(indegree[k]==0)
{
ans[index++]=k;
indegree[k]--;
break;
}
if(k>n)return ;
for(int j=1;j<=n;j++)
if(graph[k][j])
indegree[j]--;
}
}
int main()
{
int x,y,m;
while(~scanf("%d%d",&n,&m))
{
memset(graph,0,sizeof(graph));
memset(indegree,0,sizeof(indegree));
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
//1.防重边
if(graph[x][y]==0){
graph[x][y]=1;
indegree[y]++;
}
}
index=1;
topsort();
printf("%d",ans[1]);
for(int j=2;j<index;j++)
printf(" %d",ans[j]);
printf("\n");
}
return 0;
}
last
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
using namespace std;
#define M 100002
#define INF 1<<28
int n;
int father[M];
int Find(int x)
{
if(x==father[x])
return x;
return father[x]=Find(father[x]);
}
struct line{
int y,dist;
}l;
vector<line> edge[M*10];//保存全图
int main()
{
int m,x,y,dist;
int flag=0;
while(~scanf("%d%d",&n,&m)){
for(int i=1;i<=n;i++)
father[i]=i;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&l.dist);
l.y=y;
edge[x].push_back(l);
l.y=x;
edge[y].push_back(l);
if(!flag){
int tx=Find(x);
int ty=Find(y);
father[tx]=ty;
if(tx==ty)flag=1;//判断无向图有环
}
}
if(flag)printf("YES\n");
else
{
//下面求出森林的直径->>max(每棵树的最大直径)
for(int i=1;i<=n;i++)
printf("%d\n",edge[i].size());
}
}
return 0;
}