pku3169Layout(spfa)

Xredman posted @ Apr 13, 2010 05:31:21 AM in Algorithm with tags SPFA pku , 1656 阅读
6738100 Xredman 3169 Accepted 408K 32MS C++ 1597B 2010-04-13 16:20:41
#include <iostream>
#include <stack>
#include <cstdio>
#include <cstring>
#include <climits>
#include <queue>
using namespace std;

const int MAXV = 1002;
const int MAXE = 2000002;
#define INF INT_MAX

int pnt[MAXE], cost[MAXE], next[MAXE];
int head[MAXV];
int dist[MAXV];
bool Visited[MAXV];
int cc[MAXV];
int cnt;
int n, ml, md;

void addEdge(int u, int v, int c)
{
	pnt[cnt] = v;//终点
	cost[cnt] = c;
	next[cnt] = head[u];
	head[u] = cnt++;
}
bool relax(int u, int v, int c)
{
	if(dist[u] != INF && dist[v] > dist[u] + c)
	{
		dist[v] = dist[u] + c;
		return true;
	}
	return false;
}
int spfa(int src)
{
	int i;
	int u, v;
	for(i = 1; i <= n; i++)
	{
		dist[i] = INF;
		Visited[i] = false;
		cc[i] = 0;//记录入队次数
	}

	dist[src] = 0;
	queue<int> Q;
	Q.push(src);
	Visited[src] = true;
	++cc[src];
	while(!Q.empty())
	{
		u = Q.front();
		Q.pop();
		Visited[u] = false;
		for(i = head[u]; i != -1; i = next[i])
		{
			v = pnt[i];
			if(relax(u, v, cost[i]) && !Visited[v])
			{
				Q.push(v);
				Visited[v] = true;
				if((++cc[v]) > n)
					return -1;//存在负权环
			}
		}
	}
	if(dist[n] == INF)
		return -2;
	return dist[n];
}

int main()
{

	int a, b, d;
	while(cin>>n>>ml>>md)
	{
		cnt = 0;
		memset(head, -1, sizeof(head));
		while(ml--)
		{
			scanf("%d%d%d", &a, &b, &d);
			if(a > b)
				swap(a, b);
			addEdge(a, b, d);
		}
		while(md--)
		{
			scanf("%d%d%d", &a, &b, &d);
			if(a < b)
				swap(a, b);
			addEdge(a, b, -d);
		}
		cout<<spfa(1)<<endl;;
	}
	return 0;
}

 






AP 1st Inter Botany 说:
Sep 08, 2022 12:29:23 AM

In intermediate education, Botany is a very important subject for science group students for part-1 and part-2 first and the second year of junior and senior intermediate students, and the BIEAP has conducted the General Science examination tests including botany subject, download AP inter Botany Model Paper 2023 AP 1st Inter Botany Model Paper Pdf with solutions for all 1st and 2nd-year students for paper-1 and paper-2 exams 2023.

UP Board 12th Class 说:
Aug 31, 2023 08:57:58 PM

Board of High School and Intermediate Education Uttar Pradesh Aalso know (UPMSP) Board is Going to conduct the Intermediate Examination in the month of March 2024 under Uttar Pradesh state Government Administered Autonomous Examining Authority for the Standard 12th Class Examination of Uttar Pradesh, headquartered in PrayagrajIndia, UPMSP is Published in 12th Class Examination UP Board 12th Class Question Paper 2024 Date Sheet at Official Website. UPMSP has Upload Official Website the UP Board 12th Class Model Paper 2024 for Pdf Arts, Science, Commerce Stream All Subjects Pdf Format, We are Providing here the All Subjects in Guess Paper, Students must analyze the UP Board 12th Class Important Questions 2024 to Understand the Overall Design UPMSP Exam Preparations According to the Same and obtain high scores in Exam.

jnanabhumiap.in 说:
Jan 25, 2024 07:04:07 AM

JNANABHUMI AP provides a CBSE syllabus for all classes for the academic year 2024 has been designed as per the guidelines of the CBSE Board. The syllabus offers a conceptual background and lays the groundwork for the Class 10 Board exams. jnanabhumiap.in By visiting the page, students will find the downloadable pdf of the reduced CBSE 10th Syllabus along with the deleted portion of the syllabus for each subject. So, students are advised to prepare for the exam, as per the syllabus mentioned here.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter