题目图如下所示:
代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define maxn 1000
using namespace std;
struct node
{
int v, cost;
node(int _v, int _cost): v(_v), cost(_cost) {}
};
int ve[maxn];
int vl[maxn];
int n, m;
int indegree[maxn];
vector<node> adj[maxn];
vector<int> path[maxn];
stack<int> s;
int j = 0; //汇点的编号
bool topologicalsort()
{
queue<int> q;
for(int i = 1; i <= n; i++)
{
if(indegree[i] == 0)
{
q.push(i);
}
}
while(!q.empty())
{
int u = q.front();
q.pop();
s.push(u);
for(int i = 0; i < adj[u].size(); i++)
{
int v = adj[u][i].v;