za

来源:互联网 发布:js控制多行tr显示隐藏 编辑:程序博客网 时间:2024/05/14 14:19
/////////////////////////////////////                    小蓝书的内容////  main.cpp//  testTrue////  Created by sj wang on 30/04/2017.// 树的dsf遍历//使用数组存树的连接关系#include <iostream>using namespace std;//book记录顶点是否被访问过了//n 是顶点个数//m 是边个数int book[101],sum,n,m,e[101][101];//cur 是当前所在顶点的编号void dfs(int cur){    cout<<"Current is:"<<cur<<endl;    int i;    sum++;    if(sum==n){return;}    //从1到n号顶点依次尝试,看看哪些与cur相连    for(i=1; i<=n;i++){        //有路,且该顶点没有被访问过        if(e[cur][i]==1 && book[i]==0){            book[i]=1;            dfs(i);        }        return;    }}int main(){    m=5;    n=5;    e[1][2]=1;    e[1][3]=1;    e[1][5]=1;    e[2][4]=1;    e[3][5]=1;    book[1]=1;    dfs(1);    system("pause");    return 0;}////  main.cpp//  testTrue////  Created by sj wang on 30/04/2017.//// Page 151// Folyd-Warshell find shortest path// 求有向图任意两点间的最短距离#include <iostream>using namespace std;int e[101][101];int inf=999;// input: the map, size mxmvoid floyWarshellPath(int *e,int m, int inf){    for(int k=1;k<=m; k++){        for(int j=1; j<=m; j++){            for(int i=1; i<=m; i++){                if(e[i][j]>e[i][k] + e[k][j] && e[i][k]<inf && e[k][j]<inf){                    e[i][j]=e[i][k]+e[k][j];                }            }        }    }    for(int q=1; q<m; q++){        for(int p=1; p<m; p++){            cout<<e[q][p]<<"   ";        }        cout<<endl;    }}int main(){    e[1][1]=0;e[1][2]=2;e[1][3]=6;e[1][4]=4;    e[2][1]=inf;e[2][2]=0;e[2][3]=3;e[2][4]=inf;    e[3][1]=7;e[3][1]=inf;e[3][1]=0;e[3][1]=1;    e[4][1]=5;e[4][2]=inf;e[4][3]=12;e[4][4]=0;    system("pause");    return 0;}// NOT TESTED//  main.cpp//  testTrue////  Created by sj wang on 30/04/2017.//  镖局运输:最小生成树,并查集,快排//  将整个地图打通,全局看花费最少//  先将代价排序,再依次选N-1个,如果形成回路了就跳过这个// 输入各点的连接关系,如2 4 11,代表2连接4,且权重是11// 输出生成树以后的总权重#include <iostream>using namespace std;struct edge{    int u;// a side    int v;// a side    int w;// wight of the two sides}e[10];int n,m;int f[7]={0};int sum=0;int count=0;// sort from index left to rightvoid quicksort(int left, int right){    int i,j;    struct edge t;    if(left>right){return;}    i=left; j=right;    while(i!=j){        while(e[j].w>=e[left].w && i<j){j--;}        while(e[i].w<=e[left].w && i<j){i++;}        if(i<j){            t=e[i];            e[i]=e[j];            e[j]=t;        }    }    t=e[left];    e[left]=e[i];    e[i]=t;    quicksort(left, i-1);    quicksort(i+1, right);    return;}int getFather(int v){    if(f[v]==v){return v;}    else{        f[v] = getFather(f[v]);        return f[v];    }}int merge(int v, int u){    int t1,t2;    t1=getFather(v);    t2=getFather(u);    if(t1!=t2){f[t2]=t1; return 1;}    return 0;}int main(){  n=6; m=9;//把e[i].u, e[i].v, e[i].w都赋好值,分别代表点u连着v,权重是wquicksort(1,m);    //m: num of edges    for(int i=1; i<=m; i++){        if(merge(e.[i].u,e[i].v){            countN++;            sum=sum+e[i].w;        }        if(countN==n-1){break;}    }    system("pause");    return 0;}// FULL TESTED//  main.cpp//  testTrue////  Created by sj wang on 30/04/2017.//  图的割点//  用邻接矩阵存连接关系,输入:1 4 1 3 4 2 3 2 2 5 2 6 5 6 //  输出割点序号:2#include <iostream>using namespace std;int n,m,e[9][9],root;int num[9],low[9],flag[9];int indexx;//return smaller one of the twoint min(int a, int b){    return a<b ? a:b;}void dfs(int cur, int father){    int child=0;    int i;    indexx++;    num[cur]=indexx;    low[cur]=indexx;    for(i=1; i<=n; i++){        if(e[cur][i]==1){            if(num[i]==0){                child++;                dfs(i,cur);                low[cur]=min(low[cur],low[i]);                if(cur!=root && low[i]>=num[cur]){flag[cur]=1;}                if(cur==root && child==2){flag[cur]=1;}            }            else if(i!=father){                low[cur]=min(low[cur],num[i]);            }        }    }    return;}int main(){    n=6; m=7;    for(int i=1; i<=n; i++){        for(int j=1; j<=n; j++){            e[j][i]=0;}    }    //    //    //    e[1][4]=1;e[4][1]=1;e[1][3]=1;e[3][1]=1;    e[4][2]=1;e[2][4]=1;e[3][2]=1;e[2][3]=1;    e[2][5]=1;e[5][2]=1;e[2][6]=1;e[6][2]=1;    e[5][6]=1;e[6][5]=1;    //    //    //    root=1;    //从一号顶点开始dfs    dfs(1,root);    //如果是割点就打印出来    for(int i=1; i<=n; i++){        if(flag[i]==1){            cout<<"cut point is : "<<i<<endl;        }    }    //system("pause");    cout<<"Tne "<<endl;    getchar();getchar();    return 0;}// FULL TESTED//  main.cpp//  testTrue////  Created by sj wang on 30/04/2017.//  图的割边//  用邻接矩阵存连接关系e//  输入: 1 4 1 3 4 2 3 2 2 5 5 6, 表示1与4相连,1与3。。。//  输出割点序号:5-6 2-5#include <iostream>using namespace std;int n,m,e[9][9],root;int num[9],low[9],flag[9];int indexx;//return smaller one of the twoint min(int a, int b){    return a<b ? a:b;}void dfs(int cur, int father){    int i;    indexx++;    num[cur]=indexx;    low[cur]=indexx;    for(i=1; i<=n; i++){        if(e[cur][i]==1){            if(num[i]==0){                dfs(i,cur);                low[cur]=min(low[i],low[cur]);                if(low[i]>num[cur]){cout<<cur<<"-"<<i<<endl;}            }            else if(i!=father){                low[cur]=min(low[cur],num[i]);            }        }    }    return;}int main(){    n=6; m=7;    for(int i=1; i<=n; i++){        for(int j=1; j<=n; j++){            e[j][i]=0;}    }    //    //    //    e[1][4]=1;e[4][1]=1;e[1][3]=1;e[3][1]=1;    e[4][2]=1;e[2][4]=1;e[3][2]=1;e[2][3]=1;    e[2][5]=1;e[5][2]=1; e[5][6]=1;e[6][5]=1;    //    //    //    root=1;    //从一号顶点开始dfs    dfs(1,root);    //system("pause");    cout<<"Tne "<<endl;    getchar();getchar();    return 0;}// FULL TESTED//  main.cpp//  testTrue////  Created by sj wang on 30/04/2017.//  二分配对的最大值//  用邻接矩阵存连接关系,dfs//  输出最大对数: sum=3//  输入匹配关系,存到矩阵e中:1 4 1 5 2 5 2 6 3 4 ,表示1与4相连,1与5.。。。。。#include <iostream>using namespace std;int e[101][101];int match[101];int book[101];int n,m;int dfs(int u){    for(int i=1; i<=n; i++){        if(book[i]==0 && e[u][i] == 1){            book[i]=1;            if(match[i]==0 || dfs(match[i])){                match[i]=u; match[u]=i;                return 1;            }        }    }    return 0;}int main(){    //    //    //    e[1][4]=1;e[4][1]=1;e[1][5]=1;e[5][1]=1;    e[5][2]=1;e[2][5]=1;e[6][2]=1;e[2][6]=1;    e[3][4]=1;e[4][3]=1;    //    //    //    int sum=0;    // n pionts, m edges    n=6; m=5;    for(int i=1; i<=n; i++) match[i]=0;    for(int i=1; i<=n; i++){        for(int j= 1; j<=n; j++){            book[j]=0;        }        if(dfs(i)) sum++;    }    cout<<"Sum is : "<<sum<<endl;    cout<<"Tne "<<endl;    getchar();getchar();    return 0;}////////////////////////////////////                        其他
0 0
原创粉丝点击