hdoj 2121 Ice_cream’s world II 【無根節點最小樹形圖】,hdojice_cream

來源:互聯網
上載者:User

hdoj 2121 Ice_cream’s world II 【無根節點最小樹形圖】,hdojice_cream

題目:hdoj 2121 Ice_cream’s world II 


題意:題目是一道躶題,給n個點,m條邊的有向圖,然後找一個點,到所有點的距離和最小,找出這個點並輸入距離。


分析:很明顯是求一個最小樹形圖,但是沒有說根節點,要找跟節點,我們可以虛擬一個節 點 x ,x 到所有節點連邊距離為前面所有距離和+1為 dis 。

然後從x 節點求一次最小樹形圖為ans,則ans - dis 就是最小樹形圖的距離。

如果圖不連通,或者ans》=2*dis 說明不存在,都則與 x 點出發的邊就是結果點


AC代碼:

#include <iostream>#include <algorithm>#include <string>#include <math.h>#include <vector>#include <cstring>#include <cstdio>using namespace std;const int N = 1100;const int inf = 0x3f3f3f3f;int n,m;struct Node{    int from,to,dis;};vector<Node> e;int ha[N],vis[N],father[N],in[N];int Minroot;int zhuliu(int root){    int ans = 0;    while(true)    {        for(int i=0;i<n;i++)            in[i] = inf;        memset(father,-1,sizeof(father));        for(int i=0; i<e.size(); i++) //找最小入邊        {            int to = e[i].to;            if(e[i].dis<in[to] && e[i].from!=e[i].to)            {                in[to] = e[i].dis;                father[to] = e[i].from;                if(e[i].from == root) //找最小                    Minroot = i;            }        }        for(int i=0;i<n;i++)        {//printf("%d ",in[i]);            if(i!=root && in[i]==inf)                return -1;        }        int cnt = 0;        in[root] = 0;        memset(ha,-1,sizeof(ha));        memset(vis,-1,sizeof(vis));        for(int i=0;i<n;i++) //找自環        {            ans += in[i];            int v = i;            while(v!=root && ha[v]==-1 && vis[v]!=i)            {                vis[v] = i;                v = father[v];            }            if(v!=root && ha[v]==-1)            {                for(int j = father[v];j != v;j=father[j])                {                    ha[j] = cnt;                }                ha[v] = cnt++;            }        }        if(cnt == 0) //跳出條件            break;        for(int i=0;i<n;i++)            if(ha[i]==-1)                ha[i]=cnt++;        for(int i = 0; i< e.size();i++)        {            int tmp = e[i].to;            e[i].from = ha[e[i].from];            e[i].to = ha[e[i].to];            if(e[i].from != e[i].to)                e[i].dis -= in[tmp];        }        n = cnt;        root = ha[root];    }    return ans;}int main(){    //freopen("Input.txt","r",stdin);    while(~scanf("%d%d",&n,&m))    {        int num = 0;        for(int i=0;i<m;i++)        {            int x,y,s;            scanf("%d%d%d",&x,&y,&s);            e.push_back((Node){x,y,s});            num = num + s;        }        num++;        for(int i=0;i<n;i++)            e.push_back((Node){n,i,num});        n++;        int ans = zhuliu(n-1);        //printf("%d %d\n",ans,num);         if(ans==-1||ans>=2*num)            puts("impossible");        else            printf("%d %d\n",ans-num,Minroot-m);        puts("");        e.clear();    }    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.