(最小生成树+思维)城市建设

  历届试题 城市建设  

时间限制:1.0s   内存限制:256.0MB

    

问题描述

  栋栋居住在一个繁华的C市中,然而,这个城市的道路大都年久失修。市长准备重新修一些路以方便市民,于是找到了栋栋,希望栋栋能帮助他。

  C市中有n个比较重要的地点,市长希望这些地点重点被考虑。现在可以修一些道路来连接其中的一些地点,每条道路可以连接其中的两个地点。另外由于C市有一条河从中穿过,也可以在其中的一些地点建设码头,所有建了码头的地点可以通过河道连接。

  栋栋拿到了允许建设的道路的信息,包括每条可以建设的道路的花费,以及哪些地点可以建设码头和建设码头的花费。

  市长希望栋栋给出一个方案,使得任意两个地点能只通过新修的路或者河道互达,同时花费尽量小。

输入格式

  输入的第一行包含两个整数n, m,分别表示C市中重要地点的个数和可以建设的道路条数。所有地点从1到n依次编号。
  接下来m行,每行三个整数a, b, c,表示可以建设一条从地点a到地点b的道路,花费为c。若c为正,表示建设是花钱的,如果c为负,则表示建设了道路后还可以赚钱(比如建设收费道路)。
  接下来一行,包含n个整数w_1, w_2, …, w_n。如果w_i为正数,则表示在地点i建设码头的花费,如果w_i为-1,则表示地点i无法建设码头。
  输入保证至少存在一个方法使得任意两个地点能只通过新修的路或者河道互达。

输出格式

  输出一行,包含一个整数,表示使得所有地点通过新修道路或者码头连接的最小花费。如果满足条件的情况下还能赚钱,那么你应该输出一个负数。

样例输入

5 5
1 2 4
1 3 -1
2 3 3
2 4 5
4 5 10
-1 10 10 1 1

样例输出

9

样例说明

  建设第2、3、4条道路,在地点4、5建设码头,总的花费为9。

数据规模和约定

  对于20%的数据,1<=n<=10,1<=m<=20,0<=c<=20,w_i<=20;
  对于50%的数据,1<=n<=100,1<=m<=1000,-50<=c<=50,w_i<=50;
  对于70%的数据,1<=n<=1000;
  对于100%的数据,1 <= n <= 10000,1 <= m <= 100000,-1000<=c<=1000,-1<=w_i<=1000,w_i≠0。
 

首先是对题目的理解:如果有某俩个结点不通,我们可以通过在这俩个地方架设码头,只要有码头的地方都是相通的;

1:我们可以通过kruskal算法求出花费最少的路径

2:怎么处理架设码头的点呢?我们可以另外架设一个结点0,将所有假设码头的点和0结点相连然后在处理;

3:因为架设码头会多出一个点,所以我们要分开判断,以防止结点数的判断上出问题

{

1>:如果不架设码头就可以联通,我们取架设码头和不架设码头的最小值;

2>:如果不架设码头不连通,则直接在架设码头的情况下求解;

有的人也许会疑问,为什么不直接放到一起求呢?

因为架设码头的话会多出一个结点,因此如果直接求,我们的kruskal就会多出一个路径用来包含多出的结点,此时如果直接放到一起求,就有可能使结果变大;所以要分开求;

}

4:最坑的是有的路径可以盈利,为了使花费最小,这些路径不管怎么样都要加上,不能像正常的kruskal算法因为已经有了更小的连接就不加了;

题解     原文:https://blog.csdn.net/buctyyzyn/article/details/44945471 

 

在看题解前,一直是50分,看后恍然大悟!

(1) 50分

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
#define Catalan C(2n,n)-C(2n,n-1)  (1,2,5,14,42,132,429...) // 卡特兰数
using namespace std;inline int read(){char c = getchar(); int x = 0, f = 1;while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();return x * f;
}typedef long long ll;
const double pi = atan(1.)*4.;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int M=63;
const int N=1e5+5;
int n,m;struct fun{int x,y,w;
}f[N];
int a[N],per[N];
int findd(int x){if(x==per[x]) return x;return per[x]=findd(per[x]);
}bool U(int x,int y){int xx=findd(x);int yy=findd(y);if(xx!=yy){per[xx]=yy;return true;}return false;
}bool cmp(fun a,fun b){return a.w

(2)100分

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
#define Catalan C(2n,n)-C(2n,n-1)  (1,2,5,14,42,132,429...) // 卡特兰数
using namespace std;inline int read(){char c = getchar(); int x = 0, f = 1;while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();return x * f;
}typedef long long ll;
const double pi = atan(1.)*4.;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int M=63;
const int N=1e5+5;
int n,m,p,num;struct fun{int x,y,w;
}f[N];
int a[N],per[N];
int findd(int x){if(x==per[x]) return x;return per[x]=findd(per[x]);
}bool U(int x,int y){int xx=findd(x);int yy=findd(y);if(xx!=yy){per[xx]=yy;return true;}return false;
}bool cmp(fun a,fun b){return a.w

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章