URAL 1416 Confidential(次小生成树)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1416

Zaphod Beeblebrox — President of the Imperial Galactic Government. And by chance he is an owner of enterprises that trade in secondhand pens. This is a complicated highly protable and highly competitive business. If you want to stay a leader you are to minimize your expenses all the time. And the presedent's high post helps in those aairs. But he is to keep this business in secret. As a president Zaphod has access to the top secret and important information an exact value of power loss in the hyperspace transition between the planets. Of course, this information is very useful to his company. Zaphod is to choose the minimal possible set of trans-planet passages so that he could pass from any planet to any other one via those passages and their total cost would be minimal. The task won't be complicated if Zaphod was not to keep in secret that he helps his company with the secret information. Thus, Zaphod decided to find not the cheapest passages set but the next one. As a real businessman he wants to estimate the value of his conspiracy expenses.

Input

The first input line contains two integers:  N (2 ≤  N ≤ 500) is a number of planets in the Galaxy and  M is an amount of possible transitions. The next  M lines contain three integers  aibi the numbers of the planets that are connected with some passage (1 ≤  aibi ≤  N), and  wi (0 ≤  wi ≤ 1000) is the transition cost. If an  A to  B transition is possible then a  B to  A transition is possible, too. The cost of those transitions are equal. There is not more than one passage between any two planets. One can reach any planet from any other planet via some chain of these passages.

Output

You should find two different sets of transitions with the minimal possible cost and output theirs costs. Print the minimal possible cost first. If any of those sets of transitions does not exist denote it's cost by −1.

 

题目大意:给一个n个点m条边的无向图,求最小生成树和次小生成树(图无重边,似乎是连通图)。

思路:参考2014年汪汀的IOI集训队论文《最小生成树问题的拓展》。时间复杂度为O(n^2)。

 

代码(0.109MS):

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <iostream>
 5 using namespace std;
 6 
 7 const int MAXV = 510;
 8 const int MAXE = MAXV * MAXV;
 9 const int INF = 0x3f3f3f3f;
10 
11 int head[MAXV], ecnt;
12 int to[MAXE], next[MAXE], cost[MAXE];
13 bool select[MAXE];
14 int n, m;
15 
16 void init() {
17     memset(head + 1, -1, n * sizeof(int));
18     ecnt = 0;
19 }
20 
21 void add_edge(int u, int v, int c) {
22     to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
23     to[ecnt] = u; cost[ecnt] = c; next[ecnt] = head[v]; head[v] = ecnt++;
24 }
25 
26 int dis[MAXV], pre[MAXV];
27 int maxPath[MAXV][MAXV];
28 bool vis[MAXV];
29 
30 int prim() {
31     memset(dis + 1, 0x3f, n * sizeof(int));
32     dis[1] = 0;
33     int res = 0;
34     for(int _ = 0; _ < n; ++_) {
35         int u = -1;
36         for(int i = 1; i <= n; ++i) if(!vis[i] && dis[i] < INF)
37             if(u == -1 || dis[i] < dis[u]) u = i;
38         if(u == -1) return -1;
39         for(int p = head[u]; ~p; p = next[p]) {
40             int &v = to[p];
41             if(vis[v]) continue;
42             if(cost[p] < dis[v]) dis[v] = cost[p], pre[v] = p;
43         }
44         for(int i = 1; i <= n; ++i) if(vis[i]) {
45             int &v = to[pre[u] ^ 1];
46             maxPath[i][u] = maxPath[u][i] = max(maxPath[i][v], dis[u]);
47         }
48         res += dis[u];
49         vis[u] = true;
50         if(u != 1) select[pre[u]] = select[pre[u] ^ 1] = true;
51     }
52     return res;
53 }
54 
55 int solve(int ans) {
56     int res = -1;
57     for(int u = 1; u <= n; ++u) {
58         for(int p = head[u]; ~p; p = next[p]) {
59             int &v = to[p];
60             if(select[p] || u == v) continue;
61             if(res == -1 || ans - maxPath[u][v] + cost[p] < res)
62                 res = ans - maxPath[u][v] + cost[p];
63         }
64     }
65     return res;
66 }
67 
68 int main() {
69     scanf("%d%d", &n, &m);
70     init();
71     for(int i = 0, a, b, c; i < m; ++i) {
72         scanf("%d%d%d", &a, &b, &c);
73         add_edge(a, b, c);
74     }
75     int ans1 = prim(), ans2 = -1;
76     if(ans1 != -1) ans2 = solve(ans1);
77     printf("Cost: %d\n", ans1);
78     printf("Cost: %d\n", ans2);
79 }
View Code

转载于:https://www.cnblogs.com/oyking/p/3865646.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自动控制节水灌溉技术的高低代表着农业现代化的发展状况,灌溉系统自动化水平较低是制约我国高效农业发展的主要原因。本文就此问题研究了单片机控制的滴灌节水灌溉系统,该系统可对不同土壤的湿度进行监控,并按照作物对土壤湿度的要求进行适时、适量灌水,其核心是单片机和PC机构成的控制部分,主要对土壤湿度与灌水量之间的关系、灌溉控制技术及设备系统的硬件、软件编程各个部分进行了深入的研究。 单片机控制部分采用上下位机的形式。下位机硬件部分选用AT89C51单片机为核心,主要由土壤湿度传感器,信号处理电路,显示电路,输出控制电路,故障报警电路等组成,软件选用汇编语言编程。上位机选用586型以上PC机,通过MAX232芯片实现同下位机的电平转换功能,上下位机之间通过串行通信方式进行数据的双向传输,软件选用VB高级编程语言以建立友好的人机界面。系统主要具有以下功能:可在PC机提供的人机对话界面上设置作物要求的土壤湿度相关参数;单片机可将土壤湿度传感器检测到的土壤湿度模拟量转换成数字量,显示于LED显示器上,同时单片机可采用串行通信方式将此湿度值传输到PC机上;PC机通过其内设程序计算出所需的灌水量和灌水时间,且显示于界面上,并将有关的灌水信息反馈给单片机,若需灌水,则单片机系统启动鸣音报警,发出灌水信号,并经放大驱动设备,开启电磁阀进行倒计时定时灌水,若不需灌水,即PC机上显示的灌水量和灌水时间均为0,系统不进行灌水。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值