标签:detail break 根据 print 不能 节点 扩大 open 情况
最小树形图,却不知道怎么求n的父亲,直到看到一篇大佬题解;http://blog.csdn.net/mr__kid/article/details/77371066
#include <cstdio> #include <cstring> #include<algorithm> using namespace std; const int MAXNODE = 1010; const int MAXEDGE = 10010; typedef int Type; const Type INF = 0x3f3f3f3f; struct Edge { int u, v; Type dis; Edge() {} Edge(int u, int v, Type dis): u(u), v(v), dis(dis) {} }; struct Directed_MT { int n, m; Edge edges[MAXEDGE]; int vis[MAXNODE]; int pre[MAXNODE]; int id[MAXNODE]; Type in[MAXNODE]; void init(int n) { this->n = n; m = 0; } void AddEdge(int u, int v, Type dis) { edges[m++] = Edge(u, v, dis); } Type DirMt(int root) { Type ans = 0; while (1) { for (int i = 0; i < n; i++) in[i] = INF; for (int i = 0; i < m; i++) { int u = edges[i].u; int v = edges[i].v; if (edges[i].dis < in[v] && u != v) { in[v] = edges[i].dis; pre[v] = u; } } for (int i = 0; i < n; i++) { if (i == root) continue; if (in[i] == INF) return -1; } int cnt = 0;//记录缩点 memset(id, -1, sizeof(id)); memset(vis, -1, sizeof(vis)); in[root] = 0;//树根不能有入边 for (int i = 0; i < n; i++) { ans += in[i]; int v = i; while (vis[v] != i && id[v] == -1 && v != root) { vis[v] = i; v = pre[v]; } if (v != root && id[v] == -1) { for (int u = pre[v]; u != v; u = pre[u]) id[u] = cnt; id[v] = cnt++; } } if (cnt == 0) break; for (int i = 0; i < n; i++) if (id[i] == -1) id[i] = cnt++; for(int i = 0;i < m;) { int v = edges[i].v; edges[i].u = id[edges[i].u]; edges[i].v = id[edges[i].v]; if(edges[i].u != edges[i].v) edges[i++].dis -= in[v]; else swap(edges[i],edges[--m]); } n = cnt; root = id[root]; } return ans; } }MT; int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); int T;scanf("%d",&T); while(T--) { int n,m; scanf("%d%d",&n,&m); MT.init(n); while(m--) { int x,y,w; scanf("%d%d%d",&x,&y,&w); if(y==n)w=-1000*w+x; else w*=-1000; MT.AddEdge(x-1,y-1,w); } int ans= MT.DirMt(0); printf("%d %d\n", (-ans+999)/1000, (-ans+999)/1000*1000+ans); } return 0; }
标签:detail break 根据 print 不能 节点 扩大 open 情况
原文地址:http://www.cnblogs.com/MeowMeowMeow/p/7392516.html