洛谷 P1194 买礼物
来源:互联网 发布:申请淘宝api接口流程 编辑:程序博客网 时间:2024/04/27 16:18
题目来源https://www.luogu.org/problem/show?pid=1194
先设立一个虚构的点,该点与所有点均有边相连,且边权值为A,即商品的价格。
这样保证至少先买一件商品,再优惠。
存储每一条边,用Kruskal算法计算最小生成树的所有边权总和。
#include <algorithm>#include <iostream>#include <cstring>#include <sstream>#include <cstdlib>#include <string>#include <cstdio>#include <cctype>#include <vector>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <set>#include <map>using namespace std;int g[501][501]={0};const int maxn=2e5;int v[maxn],u[maxn],w[maxn],r[maxn],p[maxn];const int cmp(const int i,const int j){return w[i]<w[j];}int find(int x){return p[x]==x?x:p[x]=find(p[x]);}int main(){ ios::sync_with_stdio(false); int a,b;cin>>a>>b; for(int i=1;i<=b;i++) { for(int j=1;j<=b;j++) { cin>>g[i][j]; } } int d=0; for(int i=1;i<=b;i++) { for(int j=1;j<i;j++) { if(g[i][j]!=0) { v[++d]=i;u[d]=j; w[d]=g[i][j]; } } w[++d]=a; v[d]=i;u[d]=b+1; } b++; for(int i=1;i<=d;i++)r[i]=i; for(int i=1;i<=b;i++)p[i]=i; sort(r+1,r+1+d,cmp); int ans=0; for(int i=1;i<=d;i++) { int e=r[i]; int x=find(v[e]); int y=find(u[e]); if(x!=y) { p[x]=y; ans+=min(w[e],a); } } cout<<ans; return 0;}
0 0
- 洛谷 P1194 买礼物
- 洛谷P1194 买礼物
- 洛谷P1194 买礼物
- tmk买礼物
- zzuli 2126: tmk买礼物(模拟)
- zzuli 2126 tmk买礼物 思维题
- 9.9 买礼物的艰辛 2719
- SSL P2719 买礼物的艰辛
- GDUT2017校赛:Problem H: tmk买礼物(思维)
- 广工ProblemH 1231 TMK买礼物(判定数组)
- 2017广东工业大学 校赛H题tmk买礼物
- 2017 年广工大的程序设计决赛H题 --- tmk买礼物
- 2017年9月9日普级组 买礼物的艰辛
- cqyz#p1194 数独
- SSL P1194 最优乘车
- 礼物
- 礼物
- 礼物
- ListView性能优化
- maven核心文件:pom.xml详解
- UVA 11987 Almost Union-Find(带删除的并查集)
- Qt Quick开发中利用qml的单例模式(1)
- myeclipse中怎么建立类之间的关系图
- 洛谷 P1194 买礼物
- poj 2059 龟兔赛跑 DP
- Java中split以·点分割的问题
- hdu 5927 DFS
- 自定义控件之ListView下拉刷新
- thinkphp5 笔记
- thinkphp连接数据库配置
- 欢迎使用CSDN-markdown编辑器
- SwipeMenuListView滑动删除框架