【算法】最小生成树

【题解】

想到网络流,但是好像不能处理流量和费用的关系。

想到最短路,但好像不能处理重复选边的问题。

每条边只需要选一次,每个点就要遍历到,可以想到最小生成树。

建超级源向每个点连边,点与点连边,对n+1个点求最小生成树(MST)即可。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
int n,first[maxn],tot,fa[maxn];
struct edge{int u,v,w,from;}e[maxn*maxn*];
void insert(int u,int v,int w)
{tot++;e[tot].u=u;e[tot].v=v;e[tot].w=w;e[tot].from=first[u];first[u]=tot;}
int getfa(int x){return fa[x]==x?x:(fa[x]=getfa(fa[x]));}
bool cmp(edge a,edge b)
{return a.w<b.w;}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
int u;
scanf("%d",&u);
insert(,i,u);
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
int u;
scanf("%d",&u);
insert(i,j,u);
}
}
for(int i=;i<=n;i++)fa[i]=i;
sort(e+,e+tot+,cmp);
int cnt=,ans=;
for(int i=;i<=tot;i++)
if(getfa(e[i].u)!=getfa(e[i].v))
{
cnt++;fa[fa[e[i].u]]=fa[e[i].v];
ans+=e[i].w;
if(cnt>=n)break;
}
printf("%d",ans);
return ;
}

最新文章

  1. Tomcat绑定IPV4端口
  2. animate对颜色设置不起作用
  3. 跟开涛老师学shiro -- INI配置
  4. 查询sql 并且读取
  5. poj3468 线段树+lazy标记
  6. Nutch的发展历程
  7. 开源:矿Android新闻client,快、小、支持离线阅读、操作简单、内容丰富,形式多样展示、的信息量、全功能 等待(离开码邮箱)
  8. [转载]关于shell脚本的基本语法
  9. tomcat bio nio apr 模式性能测试
  10. 使用xcrun打包iOS应用
  11. c#中@标志的作用
  12. neo4j-cypher
  13. Windows 10 IoT Core 17101 for Insider 版本更新
  14. 如何在Linux系统下挂载光盘
  15. 我的Java学习笔记-Java面向对象
  16. 为什么有时候访问某些加密https网站是不需要证书的? https? ssl?
  17. [SoapUI]怎样从应答报文中获取某个字段的值,然后用其改写某个变量
  18. 剑指offer(36-40)编程题
  19. Linux下IPC机制
  20. STL在算法比赛中简单应用

热门文章

  1. Delegate(QLabel和QComboBox)
  2. c++远征
  3. lintcode-32-最小子串覆盖
  4. lintcode-31-数组划分
  5. LintCode-165.合并两个排序链表
  6. tomcat 相关
  7. hdu5575 Discover Water Tank
  8. 【bzoj4417】[Shoi2013]超级跳马 矩阵乘法
  9. Go语言【第二篇】:Go语法和数据类型
  10. hdu 2050 折线分割平面 (递推)