Code Vs 1003 电话连线

比较水的题目,裸prim....下面代码写得有点麻烦...仅供参考..(其实不用写那么多记录...我是蒟蒻...QAQ)


#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<functional>


using namespace std;
//------------------------------------------
const int M=105;
const int INF=0xfffffff;
//-----------------------------------------
typedef pair<int,int >P;
priority_queue<P, vector<P>, greater<P> > que;//单调队列优化prim 
//------------------------------------------
struct node
{
	int x, y;
};
node ans[M];
int map[M][M];
int n;
int sum=0;
int cost[M];
int vis[M];
int re[M];//记录连线的点的顺序 
int point[M];//记录连松弛改点的点 
//-----------------------------------------
bool cmp(node a, node b)
{
	if (a.x==b.x) return a.y<b.y;
	else return a.x<b.x;
}
void init()
{
	memset(ans, 0, sizeof(ans));
	memset(vis, 0, sizeof(vis));
	scanf("%d", &n);
	for(int i=1; i<=n; i++)
     for(int j=1; j<=n; j++)
          scanf("%d", &map[i][j]);
        
        for(int i=1; i<=n; i++) cost[i]=INF;
}
void solve()
{
	memset(re, 0, sizeof(re));
	 memset(point, 0, sizeof(point));
	 int tott=0;
	 cost[1]=0;
     que.push(P(0,1));
     while (!que.empty())
     {
     	 P s=que.top();
     	 que.pop();
     	 int v=s.second;
     	 if (cost[v] < s.first) continue;
     	 sum+=s.first;
     	 vis[v]=1;
     	 re[++tott]=v;
     	 for(int i=1; i<=n; i++)
     	 {
     	 	if (!vis[i] && i!=v && map[v][i] < cost[i])
     	 	{
     	 		cost[i]=map[v][i];
     	 		point[i]=v;
     	 		que.push(P(cost[i],i));
     	 	}
     	 }
     }
     node temp[M];
     for(int i=2; i<=n; i++)
     {
     	 
     	 ans[i-1].x=point[re[i]];
     	 ans[i-1].y=re[i];
     } 
     int tot=0;
	 for(int i=1; i<n; i++) if (map[ans[i].x][ans[i].y]) {
	 	if (ans[i].x > ans[i].y) {
	 		int tt=ans[i].x;
	 		ans[i].x=ans[i].y;
	 		ans[i].y=tt;
	 	}
	 	temp[++tot].x=ans[i].x;
	 	temp[tot].y=ans[i].y;
     }
	 printf("%d\n", tot);
	 for(int i=1; i<=tot; i++)
	 {
	 	printf("%d %d\n", temp[i].x, temp[i].y);
	 }
	 printf("%d\n",sum);
}
int main()
{
	init();
	solve();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值