図の格納テンプレート

4483 ワード

図の貯蔵のいくつかの方式
*フォワードスター
#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
",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
",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
",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 */