【BZOJ】2019: [Usaco2009 Nov]找工作(spfa)

http://www.lydsy.com/JudgeOnline/problem.php?id=2019

spfa裸题。。。。。将飞机场的费用变成负,然后spfa找正环就行了

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr2(a, b, c) for1(i, 1, b) { for1(j, 1, c) cout << a[i][j]; cout << endl; }
#define printarr1(a, b) for1(i, 1, b) cout << a[i]; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int N=250, M=600;
int ihead[N], n, m, cnt, x, s, flag, vis[N], d[N];
struct ED { int to, next, w; }e[M];
void add(int u, int v, int w) {
	e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].w=w;
}
void dfs(int u) {
	vis[u]=1;
	int v;
	for(int i=ihead[u]; i; i=e[i].next) if(d[v=e[i].to]<d[u]+e[i].w+x) {
		if(vis[v]) { flag=1; return; }
		d[v]=d[u]+e[i].w+x;
		dfs(v);
		if(flag) return;
	}
	vis[u]=0;
}
void spfa() {
	d[s]=x;
	dfs(s);
	if(flag) { puts("-1"); return; }
}
int main() {
	int t;
	read(x); read(m); read(n); read(t); read(s);
	for1(i, 1, m) {
		int u=getint(), v=getint();
		add(u, v, 0);
	}
	for1(i, 1, t) {
		int u=getint(), v=getint(), w=getint();
		add(u, v, -w);
	}
	spfa();
	if(!flag) {
		int ans=0;
		for1(i, 1, n) ans=max(ans, d[i]);
		print(ans);
	}
	return 0;
}

 

 


 

 

Description

奶 牛们没钱了,正在找工作。农夫约翰知道后,希望奶牛们四处转转,碰碰运气。而且他还加了一条要求:一头牛在一个城市最多只能赚D(1 <= D <= 1,000)美元,然后它必须到另一座城市工作。当然,它可以在别处工作一阵后又回来原来的城市再最多赚D美元。而且这样往往返返的次数没有限制。 城市间有P (1 <= P <= 150)条单向路径连接,共有C(2 <= C <= 220)座城市,编号1..C. 贝希当前处在城市S (1 <= S <= C)。路径 i 从城市A_i 到城市B_i (1 <= A_i <= C; 1 <= B_i <= C),在路径上行走不用花任何费用。为了帮助贝希,约翰让它使用他的私人飞机服务。这项服务有F条(1 <= F <= 350)航线,每条航线是从城市J_i飞到另一座城市K_i (1 <=J_i <= C; 1 <= K_i <= C),费用是T_i (1 <= T_i <= 50,000)美元。如果贝希手中如果没有现钱,可以用以后赚的钱来付机票钱。贝希可以选择任何时候,在任何城市退休。如果在工作时间上不作限制,贝希总 共可以赚多少钱呢? 如果赚的钱也不会出现限制,就输出-1。

Input

第1行: 5个空格分开的整数 D, P, C, F, S

第2..P+1行: 第 i+1行包含2个空格分开的整数,表示一条从A_i 到 B_i的单向路径

第P+2..P+F+1行: 第P+i 包含3个空格分开的整数,表示一条从J_i到K_i的单向航线,费用为T_i

Output

第1行: 在上述规则下的最多可赚的钱数。

Sample Input

100 3 5 2 1
1 5
2 3
1 4
5 2 150
2 5 120

Sample Output

250

HINT

样例说明:贝希可以从城市 1 到 5 再到 2 ,最后到 3, 总共赚 4*100 - 150 = 250 美元。

Source

posted @ 2014-09-17 12:23  iwtwiioi  阅读(534)  评论(0编辑  收藏  举报