Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Spfa+Tarjan wa 一直wa求大神解答

Posted by lmc596922196 at 2014-11-13 23:24:19 on Problem 3160
/*
* this code is made by LinMeiChen
* Problem:
* Type of Problem:
* Thinking:
* Feeling:
*/
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<list>
using namespace std;
typedef long long lld;
typedef unsigned int ud;
#define oo 0x3f3f3f3f
#define maxn 3000300
#define maxm 15001500
struct Edge
{
	int v, next;
}E[maxm],E2[maxm];
int n, tol, num, top, tol2;
int head[maxn], stack[maxn];
int low[maxn], dfn[maxn];
int dis[maxn], mark[maxn];
int id[maxn], q[maxn];
int front, rear;
int value[maxn], va[maxn];
int h[maxn];

void Init()
{
	memset(value, 0, sizeof value);
	memset(mark, 0, sizeof mark);
	memset(head, -1, sizeof head);
	memset(low, 0, sizeof low);
	memset(dfn, 0, sizeof dfn);
	memset(id, 0, sizeof id);
	memset(h, -1, sizeof h);
	tol = tol2 = num = top = 0;
}

void add_edge(int u, int v)
{
	E[tol].v = v;
	E[tol].next = head[u];
	head[u] = tol++;
}

void add_edge2(int u, int v)
{
	E2[tol2].v = v;
	E2[tol2].next = h[u];
	h[u] = tol2++;
}

void Tarjan(int u, int dep)
{
	mark[u] = 1;
	low[u] = dfn[u] = dep;
	stack[top++] = u;
	int v;
	for (int i = head[u]; i != -1; i = E[i].next)
	{
		v = E[i].v;
		if (!mark[v])
		{
			Tarjan(v, dep + 1);
			low[u] = min(low[u], low[v]);
		}
		else if (mark[v])
			low[u] = min(low[u], dfn[v]);
	}
	if (low[u] == dfn[u])
	{
		num++;
		do
		{
			id[v = stack[--top]] = num;
			//mark[v] = 0;
		} while (u != v);
	}
}

void Reach(int s)
{
	for (int i = 1; i <= n;i++)
	if (!mark[i])
		Tarjan(i, 1);
	int u, v;
	for (int i = 1; i <= n; i++)
	{
		u = i;
		for (int j = head[u]; j != -1; j = E[j].next)
		{
			int v = E[j].v;
			if (id[u] != id[v])
				add_edge2(id[u], id[v]);
		}
	}
	for (int i = 1; i <= num; i++)
		add_edge2(s, i);
	for (int i = 1; i <= n; i++)
	{
		if (va[i]>0)
			value[id[i]] += va[i];
	}
}

void Spfa(int s)
{
	for (int i = 0; i <= num; i++)
	{
		mark[i] = 0;
		dis[i] = 0;
	}
	front = rear = 0;
	q[rear++] = s;
	mark[s] = 1;
	dis[s] = 0;
	while (front < rear)
	{
		int u = q[front++];
		mark[u] = 0;
		for (int i = h[u]; i != -1; i = E2[i].next)
		{
			int v = E2[i].v;
			if (dis[u] + value[v] > dis[v])
			{
				dis[v] = dis[u] + value[v];
				if (!mark[v])
				{
					q[rear++] = v;
					mark[v] = 1;
				}
			}
		}
	}
}

int main()
{
	int m, u, v;
	while (scanf("%d%d", &n, &m) != EOF)
	{
		Init();
		for (int i = 1; i <= n; i++)
			scanf("%d", &va[i]);
		for (int i = 1; i <= m; i++)
		{
			scanf("%d%d", &u, &v);
			u++; v++;
			add_edge(u, v);
		}
		Reach(0);
		Spfa(0);
		int Max = 0;
		for (int i = 1; i <= num; i++)
		{
			Max = max(Max, dis[i]);
		}
		printf("%d\n", Max);
	}
	return 0;
}
/*
4 3
14
21
5
5
0 1
1 0
3 2
*/

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator