距离矢量路由算法实验

来源:互联网 发布:什么软件是文爱神器 编辑:程序博客网 时间:2024/06/01 10:10

计算机网络实验:

///Distance Vector routing#include<iostream>#include<cstring>#include<string>#include<vector>#include<algorithm>#include<cstdio>#include<map>#include<set>#include<cmath>#include<cctype>#include<cstdlib>#include<list>#include<iomanip>using namespace std;typedef long long LL;const int MAXN=1e4;const int INF=0x3f3f3f3f;int M[MAXN][MAXN];//存的是原始图,不能变,可以知道邻接关系int n,m;int update[MAXN];bool mark[MAXN];struct node{    int des;    int hop;    int cost;    node(int _des,int _hop,int _cost)    {        des=_des;        hop=_hop;        cost=_cost;    }};//每个点维护的一个矢量(表)vector<node> G1[MAXN],G2[MAXN];//作为新旧表交替使用void addedge(int u,int v,int c){    G1[u].push_back(node(v,v,c));    G2[u].push_back(node(v,v,c));}//A B C D E F G H//1 2 3 4 5 6 7 8void init(int n){    for(int i=1;i<=n;i++)    {        for(int j=1;j<=n;j++)        {            if(i==j) M[i][j]=0;            else M[i][j]=INF;        }    }    M[1][2]=2;M[2][1]=2;//AB    M[1][7]=6;M[7][1]=6;//AG    M[2][3]=7;M[3][2]=7;//BC    M[2][5]=2;M[5][2]=2;//BE    M[3][4]=3;M[4][3]=3;//CD    M[3][6]=3;M[6][3]=3;//CF    M[4][8]=2;M[8][4]=2;//DH    M[5][6]=2;M[6][5]=2;//EF    M[5][7]=1;M[7][5]=1;//EG    M[6][8]=2;M[8][6]=2;//FH    M[7][8]=4;M[8][7]=4;//MH}char change(int i){    return (char)('A'+i-1);}void debug(){    for(int i=1;i<=n;i++)    {        for(int j=1;j<=n;j++)        {            cout<<M[i][j]<<" ";        }        cout<<endl;    }    cout<<endl;    for(int i=1;i<=n;i++)    {        for(int j=1;j<=n;j++)        {            cout<<G1[i][j].cost<<" ";        }        cout<<endl;    }    cout<<endl;    for(int i=1;i<=n;i++)    {        for(int j=1;j<=n;j++)        {            cout<<G2[i][j].cost<<" ";        }        cout<<endl;    }    cout<<endl;}void print(int i,int cnt){    if(cnt&1)    {        for(int j=1;j<=n;j++)        {            //if(G2[i][j].cost!=INF)            cout<<(char)('A'+j-1)<<":"<<G2[i][j].cost<<" by "<<change(G2[i][j].hop)<<"  ";        }        cout<<endl;    }else    {        for(int j=1;j<=n;j++)        {            cout<<(char)('A'+j-1)<<":"<<G1[i][j].cost<<" by "<<change(G1[i][j].hop)<<"  ";        }        cout<<endl;    }}void copytable(int cnt){    for(int i=1;i<=n;i++)    {        if(update[i]<cnt) continue;        else        {            if(cnt&1)            {                for(int j=1;j<=n;j++)                {                    G1[i][j].cost=G2[i][j].cost;                    G1[i][j].hop=G2[i][j].hop;                    G1[i][j].des=G2[i][j].des;                }            }else            {                for(int j=1;j<=n;j++)                {                    G2[i][j].cost=G1[i][j].cost;                    G2[i][j].hop=G1[i][j].hop;                    G2[i][j].des=G1[i][j].des;                }            }        }    }}int main(){    ios::sync_with_stdio(false);    while(cin>>n)    {        init(n);        int u,v,c;        for(int i=0;i<=n;i++) G1[i].clear();        for(int i=0;i<=n;i++) G2[i].clear();        for(int i=0;i<=n;i++) update[i]=0;        for(int i=0;i<=n;i++) mark[i]=false;        for(int i=1;i<=n;i++)        {            G1[i].push_back(node(0,0,INF));            G2[i].push_back(node(0,0,INF));            for(int j=1;j<=n;j++)            {                if(M[i][j]!=INF)                {                    G1[i].push_back(node(j,j,M[i][j]));                    G2[i].push_back(node(j,j,M[i][j]));                }else                {                    G1[i].push_back(node(j,0,M[i][j]));//下一跳为0表示不存在                    G2[i].push_back(node(j,0,M[i][j]));                }            }        }        debug();        bool flag=false;        int cnt;//更新次数        //cnt odd g2 new g1 old        //cnt even g1 new g2 old        cout<<"original table:"<<endl;        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)            {                cout<<G1[i][j].cost<<" ";            }            cout<<endl;        }        cout<<endl;        for(cnt=1;;cnt++)        {            cout<<"*************** the "<<cnt<<"th round ********************** "<<endl;            for(int i=1;i<=n;i++) cout<<update[i]<<" ";            cout<<endl;            flag=false;            for(int i=1;i<=n;/*&&update[i]+1==cnt*/i++)            {                if(mark[i]) continue;                for(int j=1;j<=n;j++)                {                    if(i==j) continue;                    if(M[i][j]!=INF)//i,j adjacent                    {                        for(int k=1;k<=n;k++)                        {                            if(cnt&1)                            {                                if(G2[i][k].cost>M[i][j]+G1[j][k].cost)                                {                                    G2[i][k].cost=M[i][j]+G1[j][k].cost;                                    G2[i][k].hop=j;                                    G2[i][k].des=k;                                    //flag=true;                                    update[i]=cnt;                                }                            }else                            {                                if(G1[i][k].cost>M[i][j]+G2[j][k].cost)                                {                                    G1[i][k].cost=M[i][j]+G2[j][k].cost;                                    G1[i][k].hop=j;                                    G1[i][k].des=k;                                    //flag=true;                                    update[i]=cnt;                                }                            }                        }                    }                }                if(update[i]<cnt)                {                    cout<<(char)('A'+i-1)<<"'s update is over"<<endl;                    mark[i]=true;                    //cout<<"its table is:"<<endl;                }else                {                    cout<<(char)('A'+i-1)<<" is updated."<<endl;                    cout<<"its table is: "<<endl;                    print(i,cnt);                    cout<<endl;                    flag=true;                }            }            if(!flag) break;//if no update, break;            copytable(cnt);        }        cout<<endl;        cout<<"the final stable table:"<<endl;        for(int i=1;i<=n;i++)        {            cout<<change(i)<<"'s table:  ";            print(i,cnt-1);        }    }    return 0;}/*8 111 2 21 7 62 3 72 5 23 4 33 6 34 8 35 6 25 7 16 8 27 8 4*/
原创粉丝点击