图的储存 模板

图的储存的几种方式

* 前向星

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 100005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
using namespace std;
int head[maxn];
struct node{
    int from;
    int to;
    int w;
};
node edge[maxn];
bool cmp(node a,node b){
    if(a.from==b.from&&a.to==b.to)return a.w<b.w;
    if(a.from==b.from)return a.to<b.to;
    return a.from<b.from;
}
int main(){
    int n,m;
    n=m=10;
    FOR(i,0,m-
        )rd3(edge[i].from,edge[i].to,edge[i].w);
    sort(edge,edge+m,cmp);
    MT(head,-1);
    head[edge[0].from]=0;
    FOR(i,1,m-1)
        if(edge[i].from!=edge[i-1].from)
            head[edge[i].from]=i;
    FOR(i,1,n)
        for(int k=head[i];edge[k].from==i&&k<m;k++)
            printf("%d %d %d\n",edge[k].from,edge[k].to,edge[k].w);
    return 0;
}
/*
5 8 29
6 1 12
8 3 11
1 2 4
3 1 22
4 3 17
7 4 25
6 5 9
8 7 7
1 6 9
*/
* 邻接表


#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 100005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
using namespace std;
struct node{
    int to;
    int w;
};
vector <node> mapp[maxn];
int main(){
    FOR(k,1,10){
        node e;
        int i,j,w;
        cin>>i>>j>>w;
        e.to=j;
        e.w=w;
        mapp[i].push_back(e);
    }
    FOR(i,1,10){
        for(vector<node>::iterator k=mapp[i].begin();k!=mapp[i].end();k++){
            node t=*k;
            printf("%d %d %d\n",i,t.to,t.w);
        }
    }
    return 0;
}
/*
5 8 29
6 1 12
8 3 11
1 2 4
3 1 22
4 3 17
7 4 25
6 5 9
8 7 7
1 6 9
*/

* 静态建表 链式前向星

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 100005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
using namespace std;
int head[maxn];
struct node{
    int to;
    int w;
    int next;
}edge[maxn];
int main(){
    MT(head,-1);
    FOR(k,1,10){
        int i,j,w;
        rd3(i,j,w);
        edge[k].to=j;
        edge[k].w=w;
        edge[k].next=head[i];
        head[i]=k;
    }
    FOR(i,1,10)
        for(int k=head[i];k!=-1;k=edge[k].next)
            printf("%d %d %d\n",i,edge[k].to,edge[k].w);
    return 0;
}
/*
5 8 29
6 1 12
8 3 11
1 2 4
3 1 22
4 3 17
7 4 25
6 5 9
8 7 7
1 6 9
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值