URAL 1416 Confidential(次小產生樹)

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   2014   art   

題目連結: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.InputThe 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.OutputYou 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.