边没有用struct封装起来,节点和边的计数起点如果不符合习惯可以稍作修改
建图+DFS+BFS
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
int n, m, all = -1; // n:点数 m:边数 all:边的编号 (边的编号由0开始,点的编号从1开始)
int pre[maxn]; // pre[e] e这条边同一出发点的上一条出边
int last[maxn]; // last[v] 以v为起点构建的最后一条边
int to[maxn]; // to[e] e连向的点
int que[maxn]; // 数组模仿队列,用于存bfs顺序,que 从1开始计数,详见bfs函数
bool vis[maxn]; // vis[v] v是否被访问过
void Build(int x, int y, int w = 1) // 建立x->y的边,权默认1
{
pre[++all] = last[x];
last[x] = all;
to[all] = y;
cost[all] = w;
}
void input()
{
all = -1;
memset(last, -1, sizeof(last));
cin >> n >> m; // n点 m边
for (int i = 1; i <= m; i++)
{
int a,b,w;
cin >> a >&