先看一下lrj的大白书上的讲解
emm。。。我是看完之后直接看的模板题代码。。。居然看懂。。。行吧。。
就是先判断 能不能联通 如能联通 就求出每个点的最小前驱边 求完之后 看有没有环 如有环 缩点更新 然后一直重复 直至无环且联通。。
//邻接矩阵模板:
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #define rap(i, a, n) for(int i=a; i<=n; i++) #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 10010, INF = 0x7fffffff; int n, m; int vis[maxn], inc[maxn], pre[maxn]; double w[105][105]; struct edge { int x, y; }Edge[maxn]; void dfs(int u) { vis[u] = 1; for(int i=1; i<=n; i++) if(!vis[i] && w[u][i] < INF) dfs(i); } double dirmst(int u) { double ans = 0; //== 步骤1: 判断能否形成最小树形图,直接dfs遍历 (就是检验一下图是否能够联通) dfs(u); for(int i=1; i<=n; i++) if(!vis[i]) return -1; //== 如果可以形成最小树形图,继续 mem(vis, 0); while(true) { //== 1. 找最小前驱边 for(int i=1; i<=n; i++) if(i != u && !inc[i]){ w[i][i] = INF; pre[i] = i; for(int j=1; j<=n; j++) if(!inc[j] && w[j][i] < w[pre[i]][i]) pre[i] = j; } //== 2.判断是否有环 int i; for(i=1; i<=n; i++) if(i != u && !inc[i]){ int j = i, cnt = 0; while(j != u && pre[j] != i && cnt <= n) j = pre[j], ++cnt; if(j == u || cnt > n) continue; break; } //== 没有找到环,得到答案 if(i > n) { for(int i=1; i<=n; i++) if(i != u && !inc[i]) ans += w[pre[i]][i]; return ans; } //== 有环,则对这个环进行收缩 int j = i; mem(vis, 0); do{ ans += w[pre[j]][j], j = pre[j], vis[j] = inc[j] = true; }while(j != i); inc[i] = false; // 环缩成了点i,点i仍然存在 for(int k=1; k<=n; k++) if(vis[k]){ //在环中的点 for(int j=1; j<=n; j++) if(!vis[j]){ //不在环中的点 if(w[i][j] > w[k][j]) w[i][j] = w[k][j]; //更新环的出边 if(w[j][k] < INF && w[j][k] - w[pre[k]][k] < w[j][i]) //更新环的入边 w[j][i] = w[j][k] - w[pre[k]][k]; } } } return ans; } void init() { mem(vis, 0); mem(inc, 0); rap(i, 0, n) rap(j, i, n) w[i][j] = w[j][i] = INF; } int main() { while(~scanf("%d%d", &n, &m)) { init(); rap(i, 1, n) { scanf("%d%d", &Edge[i].x, &Edge[i].y); } rap(i, 1, m) { int a, b; scanf("%d%d", &a, &b); double c = sqrt((double)(Edge[a].x - Edge[b].x)*(Edge[a].x - Edge[b].x) + (double)(Edge[a].y - Edge[b].y)*(Edge[a].y - Edge[b].y)); if(w[a][b] > c) w[a][b] = c; } double ans = dirmst(1); if(ans < 0) puts("poor snoopy"); else printf("%.2f ", ans); } return 0; }
//带权结构体模板:
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #define rap(i, a, n) for(int i=a; i<=n; i++) #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 10010, INF = 0x7fffffff; int ID[maxn], IN[maxn], vis[maxn], pre[maxn]; struct node { int u, v, c, b; }Node[maxn*2]; bool dirmst(int root, int n, int m, int cost, int B) { LL ans = 0; while(true) { for(int i=0; i<n; i++) IN[i] = INF; //记录最小前驱边的值 //1、找最小前驱边 for(int i=0; i<m; i++) { int u = Node[i].u; int v = Node[i].v; if(Node[i].c < IN[v] && u != v && Node[i].b >= B) { pre[v] = u; IN[v] = Node[i].c; // cout<< e.v << " " << e.u <<endl; } } //2、判断是否联通 for(int i=0; i<n; i++) { if(i == root) continue; if(IN[i] == INF) return false; } //3、找环 int cntnode = 0; mem(ID, -1); mem(vis, -1); 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 j=pre[v]; j!=v; j=pre[j]) { ID[j] = cntnode; } ID[v] = cntnode++; } } if(cntnode == 0) break; //没有环就结束 //重新标记其它点 for(int i=0; i<n; i++) if(ID[i] == -1) ID[i] = cntnode++; for(int i=0; i<m; i++) { int v = Node[i].v; Node[i].u = ID[Node[i].u]; Node[i].v = ID[Node[i].v]; if(Node[i].u != Node[i].v) Node[i].c -= IN[v]; } n = cntnode; root = ID[root]; } if(ans <= cost) return true; return false; } int main() { int T, n, m, cost; scanf("%d", &T); while(T--) { int l = INF, r = 0; scanf("%d%d%d", &n, &m, &cost); for(int i=0; i<m; i++) { scanf("%d%d%d%d", &Node[i].u, &Node[i].v, &Node[i].b, &Node[i].c); Node[i+m] = Node[i]; l = min(l, Node[i].b); r = max(r, Node[i].b); } if(!dirmst(0, n, m, cost, l)) { printf("streaming not possible. "); continue; } while(l <= r) { int mid = l + (r - l) / 2; for(int i=0; i<m; i++) Node[i] = Node[i+m]; if(!dirmst(0, n, m, cost, mid)) r = mid - 1; else l = mid + 1; } printf("%d kbps ", r); } return 0; }