void Graph::shortestPath(int src){
	SI s;
	int dist[V];
	bool visit[V];
	fill(visit, visit + V, false);
	fill(dist, dist+V, INT_MAX);
	for(int i=0; i<V; i++){
		if(!visit[i])
			topologicalSort(i, visit, s);
	}
	dist[src] = 0;
	while(!s.empty()){
		int u = s.top();
		s.pop();
		LN::iterator iter = adj[u].begin();
		while(iter != adj[u].end()){
			int v = (*iter).v;
			if(dist[u] != INT_MAX && dist[v] > dist[u] + (*iter).weight)
				dist[v] = dist[u] + (*iter).weight;
			iter++;
		}
	}
	for(int i=0; i<V; i++){
		cout<<i<<" "<<dist[i]<<'\n';
	}
}
コード例 #2
0
int main(){

	scanf("%d%d", &noV, &noE);

	G = VVI(noV+1, VI(0, 0));
	RG = VVI(noV+1, VI(0, 0));
	ans = VVI(noV+1, VI(0,0));
	vis = VB(noV+1, false);

	for(int i = 0 ; i < noE ; i++){
		int u,v;
		scanf("%d%d", &u, &v);
		G[u].push_back(v);
		RG[v].push_back(u);
	}

	for(int i = 1 ; i <= noV ; i++){
		if(!vis[i]){
			vis[i] = true;
			dfs(i);
			st.push(i);
		}
	}

	vis = VB(noV+1, false);
	vInfoAns = VI(noV+1, -1);
	while(!st.empty()){
		int here = st.top(); st.pop();

		if(!vis[here]){
			vis[here] = true;
			ans[noAns].push_back(here);
			vInfoAns[here] = noAns;
			dfsReverse(here);
			noAns++;
			sort(ans[noAns-1].begin(), ans[noAns-1].end());
		}
	}

	vis = VB(noAns+1, false);
	printf("%d\n", noAns);
	for(int i = 1 ; i <= noV ; i++){
		int here = vInfoAns[i]; // == noAns
		if(!vis[here]){
			vis[here] = true;
			for(auto iter = ans[here].begin() ; iter != ans[here].end() ; iter++){
				printf("%d ",*iter);
			}
			puts("-1");
		}
	}

	return 0;
}
コード例 #3
0
int main(int argc, char const *argv[])
{
	int n, c, s;
	
	while(SC2(n, k) && n)
	{	
		int lt = 0;	
		ZERO(in); ZERO(out);
		
		for(int i = 1; i <= n; i++)
		{
			SC2(c, s);	
			in[c] = i;
			out[s] = i;
			lt = max(lt, s+1);
		}
		
		while(!p.empty()) 
			p.pop();
		
		int i;
		
		for(i = 1; i < lt; i++)
		{
			if(out[i]){
				if(p.empty() || p.top() != out[i]) break;
				else p.pop();
			}
			
			if(in[i]){
				if((int)p.size() < k) p.push(in[i]);
				else break;
			}
		}

		printf("%s\n", i == lt ? "Sim" : "Nao");
	}
}