POJ 3164 Command Network 最小樹形圖-朱劉演算法裸題

來源:互聯網
上載者:User

標籤:class   blog   c   code   tar   http   

題目來源:POJ 3164 Command Network

題意:求以1為根的最小樹形圖 沒有輸出字串

思路:直接高朱劉演算法 不懂的可以百度 學會了就是直接套模板的事情 其實就是不斷消圈而已 不構成圈就有解 無法從根到達其他點就無解

 

#include <cstdio>#include <cstring>#include <cmath>const int maxn = 110;const int maxm = 50010;const double INF = 999999999;struct edge{int u, v;double w;edge(){}edge(int u, int v, double w) : u(u), v(v), w(w){}}e[maxm];struct Point{double x, y;}p[maxn];int pre[maxn], vis[maxn], no[maxn];double in[maxn];double dis(Point a, Point b)  {      return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));  }  double MST(int n, int m, int rt){double ans = 0;while(1){for(int i = 1; i <= n; i++)in[i] = INF;for(int i = 1; i <= m; i++){int u = e[i].u;int v = e[i].v;if(u != v && in[v] > e[i].w){pre[v] = u;in[v] = e[i].w;}}for(int i = 1; i <= n; i++){if(i == rt)continue;if(in[i] == INF)return -1;}memset(no, -1, sizeof(no));memset(vis, -1, sizeof(vis));in[rt] = 0;int cnt = 0;for(int i = 1; i <= n; i++){ans += in[i];int v = i;while(v != rt && vis[v] != i && no[v] == -1){vis[v] = i;v = pre[v];}if(v != rt && no[v] == -1){for(int u = pre[v]; u != v; u = pre[u])no[u] = cnt+1;no[v] = cnt+1;cnt++;}}if(!cnt)return ans;for(int i = 1; i <= n; i++)if(no[i] == -1)no[i] = ++cnt;for(int i = 1; i <= m; i++){int u = e[i].u;int v = e[i].v;e[i].u = no[u];e[i].v = no[v];if(no[u] != no[v]){e[i].w -= in[v];}}n = cnt;rt = no[rt];}return ans;}int main(){int n, m;while(scanf("%d %d", &n, &m) != EOF){for(int i = 1 ; i <= n; i++)scanf("%lf %lf", &p[i].x, &p[i].y);int add = 1;for(int i = 1; i <= m; i++){scanf("%d %d", &e[add].u, &e[add].v);if(e[add].u == e[add].v)continue;e[add].w = dis(p[e[add].u], p[e[add].v]);add++;}double ans = MST(n, add-1, 1);if(ans < 0)puts("poor snoopy");elseprintf("%.2f\n", ans);}return 0;}


 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.