古老的四种图存储方式
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
int graph[100][100];
int n,m;
int x,y,w;
int main()
{
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i ++)
for(int j = 1;j <= n;j ++)
{
if(i == j)
graph[i][j] = 0;
else
graph[i][j] = 1000000;
}
while(m --)
{
scanf("%d%d%d",&x,&y,&w);
graph[x][y] = w;
}
for(int i = 1;i <= n;i ++)
for(int j = 1;j <= n;j ++)
if(graph[i][j] != 0 && graph[i][j] != 1000000)
printf("%d -> %d have %d longth\n",i,j,graph[i][j]);
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
struct Node
{
int from;
int to;
int w;
};
Node edge[10000];
int head[10000];
int n,m;
int x,y,w;
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()
{
scanf("%d%d",&n,&m);
for(int i = 1;i <= m;i ++)
{
scanf("%d%d%d",&edge[i].from,&edge[i].to,&edge[i].w);
}
sort(edge + 1,edge + m,cmp);
memset(head,-1,sizeof(head));
head[edge[0].from] = 0;
for(int i = 1;i < m;i ++)
if(edge[i].from != edge[i - 1].from)
head[edge[i].from] = i;
for(int i = 1;i <= n;i ++)
{
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);
}
}}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
struct EdgeNode
{
int to;
int w;
EdgeNode * next;
};
struct VNode
{
int from;
EdgeNode * first;
};
VNode Adjlist[1000];
int n,m;
int x,y,w;
int main()
{
scanf("%d%d%d",&n,&m);
while(m --)
{
scanf("%d%d%d",&x,&y,&w);
EdgeNode *p = new EdgeNode();
p -> to = y;
p -> w = w;
p -> next = Adjlist[x].first;
Adjlist[x].first = p;
}
for(int i = 1;i <= n;i ++)
{
for(EdgeNode * k;k != NULL;k = k -> next)
printf("%d %d have %d\n",i,k -> to,k -> w);
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
int head[10000];
struct EdgeNode
{
int to;
int w;
int next;
};
EdgeNode Edges[10000];
int n,m;
int x,y,w;
int main()
{
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
for(int k = 1;k <= m;k ++)
{
scanf("%d%d%d",&x,&y,&w);
Edges[k].to = y;
Edges[k].w = w;
Edges[k].next = head[x];
head[x] = k;
}
for(int i = 1;i <= n;i ++)
for(int k = head[i];k != -1;k = Edges[k].next)
printf("%d %d have %d\n",i,Edges[k].to,Edges[k].w);
}}