The 11th chapter graph theory model and algorithm

Source: Internet
Author: User

Talk about trees again.

No root tree has a root number

#include <bits/stdc++.h>using namespace Std;const int maxn = 100;vector<int> g[maxn];int n;void read_tree () {    int u,v;    scanf ("%d", &n);        for (int i=0;i<n-1;i++) {scanf ("%d%d", &u,&v);        G[u].push_back (v);    G[v].push_back (U);    }}int p[100];void dfs (int u,int fa) {///Convert to U as root subtree, u parent node is FA int d=g[u].size ();        for (int i=0;i<d;i++) {int v=g[u][i];    if (V!=FA) DFS (V,P[V]=U);    }}int Main () {read_tree ();        for (int i=0;i<n-1;i++) {printf ("%d:", i);        for (int j=0;j<g[i].size (); j + +) printf ("%d", g[i][j]);    printf ("\ n");    } p[1]=-1;    DFS (1,-1); for (int i=0;i<n;i++) printf ("%d", I); printf ("\ n"); <pre name= "code" class= "CPP" >const int maxn = 100;int LCH[MAXN], RCH[MAXN]; Char OP[MAXN]; The left and right child node number and character int nc=0 of each node;    node int build_tree (char * s,int x,int y) {int i,c1=-1,c2=-1,p=0;    int u;        if (y-x==1) {//Enter a character to establish a separate node u = ++NC; lch[u]=rch[u]=0;        OP[U]=S[X]; return u;   } for (i=x;i<y;i++) {switch (S[i]) {case ' (':p ++;break;            Case ') ':p--;break; Case ' + ': Case '-': if (!p) c1=i;            Break Case ' * ': Case '/': if (!p) c2=i;        Break    }} if (c1<0) c1=c2;    if (c1<0) return Build_tree (s,x+1,y-1);    U=++NC;    Lch[u]=build_tree (S,X,C1);    Ech[u]=build_tree (S,c1+1,y);    OP[U]=S[C1]; return u;}

for (int i=0;i<n;i++) printf ("%d", p[i]); printf ("\ n"); /* Initial Tree 8 1 4 1 5 1 0 5 6 5 7 0 2 0 3 0:1 2 3 1:4 5 0 2:0 3:0 4:1 5:1 6 7 6:5 * return 0;}


An expression tree

const int MAXN = 100;int LCH[MAXN], RCH[MAXN]; Char OP[MAXN]; The left and right child node number and character int nc=0 of each node; node int build_tree (char * s,int x,int y) {    int i,c1=-1,c2=-1,p=0;    int u;    if (y-x==1) {//Enter a character to establish a separate node        u = ++NC;        lch[u]=rch[u]=0; OP[U]=S[X];        return u;    }    for (i=x;i<y;i++) {        switch (S[i]) {case            ' (':p ++;break;            Case ') ':p--;break;            Case ' + ': Case '-': if (!p) c1=i; break;            Case ' * ': Case '/': if (!p) c2=i; break;        }    }    if (c1<0) c1=c2;    if (c1<0) return Build_tree (s,x+1,y-1);    U=++NC;    Lch[u]=build_tree (S,X,C1);    Ech[u]=build_tree (s,c1+1,y);    OP[U]=S[C1];    return u;}


Example 11-1 common expression elimination uva12219

#include <bits/stdc++.h>using namespace Std;const int INF = 0x3f3f3f3f;const int MAXN = 105;struct Edge {int u,v,    Cost    BOOL operator < (const edge& a) const {return cost > a.cost; }};int f[maxn];int n,m;vector<edge> edges;int Find (int x) {return x==f[x]?x:f[x]=find (f[x]);}    int Kruskal (int k) {for (int i=0;i<=n;i++) f[i]=i;    int cnt=0;    int minn=inf,maxn=0;        for (int i=k;i<m;i++) {Edge e = edges[i];        int v=e.v;        int u=e.u;        int fu=find (u);        int Fv=find (v);            if (FU!=FV) {cnt++;            F[FU]=FV;            Minn=min (E.cost,minn);        MAXN =max (E.COST,MAXN);    }} if (cnt!=n-1) return-1; else return Maxn-minn;}        int main () {while (~scanf ("%d%d", &n,&m)) {if (n==0&&m==0) break;        Edges.clear ();            for (int i=1;i<=m;i++) {int a,b,c;            scanf ("%d%d%d", &a,&b,&c); Edges.push_back (Edge) {a,b,c});        } sort (Edges.begin (), Edges.end ());        int ans=inf;            for (int i=0;i<=m;i++) {int T=kruskal (i);            if (t==-1) break;        ans = min (ans,t);        } if (ans = = INF) printf ("%d\n",-1);    else printf ("%d\n", ans); } return 0;}

Example 11-3 buy or Build uva1151

#include <bits/stdc++.h>using namespace Std;const int maxn = 1005;struct point {int x, y;} pp[maxn];struct edge{int s,e,dist;} L[maxn*maxn];int n,q,m;int p[maxn];vector<int> g[10];int c[10];int distance_ (point A,point B) {return (a.x-b.x) * ( a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y);} BOOL CMP (Edge A,edge b) {return a.dist < b.dist;} int find_ (int x) {return p[x]==x?x:p[x]=find_ (p[x]);}    BOOL Merge_ (int a,int b) {int x=find_ (a);    int Y=find_ (b);    if (x==y) return false; P[x]=y; return true;}    int Kruskal () {int ans=0;    int num=0;            for (int i=0;i<m&&num<n-1;i++) {if (Merge_ (L[I].S,L[I].E)) {num++;        Ans + = l[i].dist; }} return ans;    void Solve () {for (int i=0;i<=n;i++) p[i]=i;    int Ans=kruskal ();        for (int s=1;s< (1&LT;&LT;Q); s++) {int cost=0;        for (int tt=0;tt<=n;tt++) P[tt]=tt; for (int j=0;j<q;j++) {if (!) (            (S&GT;&GT;J) &1) continue; Cost + =C[J];            for (int k=0;k<g[j].size (); k++) {Merge_ (g[j][k],g[j][0]);    }} ans =min (ans, Cost+kruskal ()); } printf ("%d\n", ans);}    int main () {int t;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&q);        for (int i=0;i<10;i++) g[i].clear ();            for (int i=0;i<q;i++) {int cnt;            scanf ("%d%d", &cnt,&c[i]);            int A;                for (int j=0;j<cnt;j++) {scanf ("%d", &a);            G[i].push_back (a);        }} for (int i=1;i<=n;i++) {scanf ("%d%d", &pp[i].x,&pp[i].y);        } m=0;                for (int i=1;i<=n;i++) {for (int j=i+1;j<=n;j++) {l[m].s=i;                L[m].e=j;            L[m++].dist=distance_ (Pp[i],pp[j]);        }} sort (l,l+m,cmp);        Solve ();    if (t) printf ("\ n"); } return 0;}

Dijkstra algorithm:


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The 11th chapter graph theory model and algorithm

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.