Week8 Assignment-B-The cat rushes forward

topic

As we all know, TT is a heavy cat lover, he has a magical magic cat.

One day, TT watched a cat game at station B. There are a total of N cats, numbered 1, 2, 3,..., N to compete. After the competition, the Up master will rank all the cats from front to back and hand out the dried fish they like to eat. Unfortunately, at this time, TT's electronic equipment was hit by cosmic rays, and it was no longer connected to the Internet. Naturally, the final award ceremony was not visible.

Fortunately, the magic cat of TT recorded the results of each game. Now he wants to program to determine the lowest lexicographical ranking sequence. Please help him.

Input

There are several groups of input, the first line in each group is two numbers N (1<=N<=500), M; where N represents the number of cats, and M represents the next M lines of input data. In the next M rows of data, each row also has two integers P1. P2 means that the cat numbered P1 won the cat numbered P2.

Output

Give a ranking that meets the requirements. When outputting, there are spaces between the numbers of cats, and there is no space after the last one!

Other explanation: The qualified ranking may not be unique. At this time, the team with the lower number is required to be the first when outputting; the input data is guaranteed to be correct, that is, the input data must be guaranteed to have a ranking that meets the requirements.

Sample Input

4 3
1 2
2 3
4 3

Sample Output

1 2 4 3

Topological sequence (Kahn algorithm)

• Form a set S of points with an in-degree of 0
• Take a vertex u from S (you can take it randomly) into L, then traverse all edges (u, v) of vertex u, delete it, and judge If another vertex v of this edge, if the in-degree is 0 after removing this edge, then this vertex is put into the set S. Repeatedly extract the vertices and repeat the process...
• Finally, when the set is empty, check whether there are any edges in the graph. If there is, then the graph must have a loop, otherwise it returns L, and the order in L is the result of topological sorting

Ideas

• The win-loss relationship between cats can form a directed acyclic graph
• P1 winning P2 is equivalent to an edge from node P1 to node P2
• P1 winning P2 means in the final ranking sequence P1 must be in front of P2
• The problem is transformed into a topological order with the smallest lexicographic order
• All nodes in the queue at any time mean that the ranking relationship has been determined and there is no dependence on each other
• The number in the queue can be taken when taking the head of the queue The smallest point dequeue
• Use priority queue to replace queue

error

1. Note that there are several groups of input, and made this mistake again...

Code

#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
const int maxn=5e4+10;
const int maxm=5e5+10;
int inf=1e9;
// 前向星存图
int head[maxn], tot;
struct Edge{
    
    
	int to, next, w;
}e[maxm];
void add(int x,int y,int w)
{
    
    
	e[++tot].to=y;
	e[tot].next=head[x];
	e[tot].w=w;
	head[x]=tot;
}
int dis[maxn],vis[maxn];
void spfa(int s) 
{
    
    
	std::queue<int> q;
	q.push(s);
	dis[s] = 0;
	vis[s] = 1;
	while(!q.empty()) {
    
    
		int u = q.front(); q.pop();
		vis[u] = 0;
//		cout<<u<<endl;
		for(int i = head[u]; i!=0; i = e[i].next) {
    
    //不能用~i 
			int v = e[i].to;
//			cout<<u<<' '<<v<<' '<<i<<endl;			
			if(dis[v] < dis[u] + e[i].w) {
    
    
				dis[v] = dis[u] + e[i].w;
				if(!vis[v]) {
    
    
					q.push(v);
					vis[v] = 1;
				}
			}
		}
	}
}
int main()
{
    
    
	int n,a,b,c,bmax=0;
	cin>>n;
	tot=0;
	memset(head,0,sizeof(head));
	for(int i=0;i<n;i++)
	{
    
    
		cin>>a>>b>>c;
//		a++;//要将零点考虑进去 
//		b++;
		if(b>bmax)
			bmax=b;
		add(a-1,b,c);
	}
	for(int i=1;i<=bmax;i++)
	{
    
    
		add(i-1,i,0);
		add(i,i-1,-1);
		dis[i]=-inf;
		vis[i]=0;
	}
	spfa(0);
	cout<<dis[bmax]<<endl;
	return 0;
 } 

Guess you like

Origin blog.csdn.net/alicemh/article/details/105498192