Hdu2121-ice_cream ' s World II (Zhu Liu algorithm, not fixed root)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2121

The title is probably for you to choose a city in some cities to do the capital, the capital will be able to go to other cities, road costs are the least, and the road is one-way, this time will use the minimum tree diagram algorithm, and is not fixed root.

adventitious Root is added a virtual root (originally nonexistent point), you can let this virtual root to each point of the distance is greater than the original all points connected to the sum of the road cost, and then calculate the result minus sum, if larger than sum can be considered through this virtual node we have two points in the original image, that the original is not connected, We can assume that there is no minimum tree shape. So the real root, when looking for the smallest into the arc, if the starting point of this arc is a virtual root, then the end of this arc is the root of the request, because if there are many solutions, there must be a ring, the apex of the ring can do the root, but we according to the nature of the smallest into the edge, we know, if not Because the edges of the virtual root are very large. But after shrinking, it is necessary to find the root of the smallest ordinal. (Drawing comprehension)

Here's the code:

  

#include <iostream>#include<cstring>#include<cstdio>using namespacestd;Const intMAXN = 1e3 +Ten; typedefLong LongLL;intID[MAXN], VIS[MAXN], PRE[MAXN], POS; LL INF=1e17, D[MAXN];structNode {intu, V, cost;} EDGE[MAXN*MAXN]; LL Zhuliu (intRoot,intVintE) {LL res=0;  while(true) {         for(inti =0; i < V; i++) {D[i]=INF; }         for(inti =0; i < E; i++) {            intU = edge[i].u, v =edge[i].v; if(U! = v && d[v] >edge[i].cost) {D[v]=Edge[i].cost; PRE[V]=u; if(U = =root) {POS= i;//record location except here, it's not the same. Other places are the template of Zhu Liu algorithm                }            }        }         for(inti =0; i < V; i++) {            if(D[i] = = INF && I! =root) {                return-1; }        }        intCont =0; memset (ID,-1,sizeof(ID)); memset (Vis,-1,sizeof(VIS)); D[root]=0;  for(inti =0; i < V; i++) {            intv =i; Res+=D[i];  while(Id[v] = =-1&& Vis[v]! = I && v! =root) {Vis[v]=i; V=Pre[v]; }             if(Id[v] = =-1&& V! =root) {                 for(intU = pre[v]; U! = V; U =Pre[u]) {Id[u]=cont; } Id[v]= cont++; }        }        if(!cont) {             Break; }         for(inti =0; i < V; i++) {            if(Id[i] = =-1) {Id[i]= cont++; }        }         for(inti =0; i < E; i++) {            intU = edge[i].u, v =edge[i].v; EDGE[I].U=Id[u]; EDGE[I].V=Id[v]; if(Id[u]! =Id[v]) {Edge[i].cost-=D[v]; }} V=cont; Root=Id[root]; }    returnRes;}intMain () {intN, M;  while(~SCANF ("%d%d", &n, &m)) {LL sum=0;  for(inti =0; I < m; i++) {scanf (" %d%d%d", &edge[i].u, &AMP;EDGE[I].V, &edge[i].cost); EDGE[I].U+ +, edge[i].v++; Sum+=Edge[i].cost; } Sum++;//Edge weight greater than total weight value         for(inti = m; I < n + m; i++) {edge[i].u=0;//0 is a virtual nodeEDGE[I].V = I-m +1; Edge[i].cost=sum; } LL Res= Zhuliu (0, n +1, n + m);//n + one point n + M edge        if(res = =-1|| Res-sum >= sum) {//if res-sum < sum, it means that the 0 out of 1 indicates that the original is a connected graph .printf"impossible\n\n"); }        Else{printf ("%lld%d\n\n", Res-sum, pos-m); }    }}

I'm sorry, it's very rubbing.

Hdu2121-ice_cream ' s World II (Zhu Liu algorithm, not fixed root)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.