Title:
pid=2121 "target=" _blank ">hdoj 2121 Ice_cream ' s World II
Test instructions: The topic is a frolicked problem, give n points, M edge of the graph. Then find a point. Distance and minimum to all points. Find this point and enter a distance.
Analysis: It is very obvious to ask for a minimal tree diagram, but not to say the root node. To find a node, we are able to virtual a node x. X to all nodes the edge distance is the front full distance and +1 for the DIS.
Then, from the X-node, the minimum tree is the ans, then the Ans-dis is the distance of the minimum tree graph.
If the diagram is not connected, or ans "=2*dis description does not exist, then the X-point is the edge of the result point
AC Code:
#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++)//Find minimum entry side {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)//find minimum 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++)//find self ring {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 out of condition; 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;}
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Hdoj 2121 Ice_cream ' s World II "no Minimum tree root node"