1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
   | #include<bits/stdc++.h> #define RG register #define clear(x, y) memset(x, y, sizeof(x)); using namespace std;
  inline int read() { 	int data=0, w=1; 	char ch=getchar(); 	while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar(); 	if(ch=='-') w=-1, ch=getchar(); 	while(ch>='0'&&ch<='9') data=(data<<3)+(data<<1)+(ch^48), ch=getchar(); 	return data*w; }
  const int maxn(1010), maxm(10010); struct edge { int next, to, dis; } e[maxm << 2]; int head[maxn], e_num, n, ML, DL, a, b, c, dis[maxn], cnt[maxn]; bool inque[maxn]; inline void add_edge(int from, int to, int dis) { 	e[++e_num]=(edge){head[from], to, dis}; 	head[from]=e_num; }
  queue<int> q; inline int spfa() { 	inque[1]=cnt[1]=1; dis[1]=0; 	q.push(1); 	while(!q.empty()) 	{ 		int x=q.front(); q.pop(); 		if(cnt[x]>=n) return -1; 		for(RG int i=head[x];i;i=e[i].next) 		{ 			int to=e[i].to; 			if(dis[x]+e[i].dis<dis[to]) 			{ 				dis[to]=dis[x]+e[i].dis; 				if(!inque[to]) 				{ 					inque[to]=true; ++cnt[to]; 					if(cnt[to]>=n) return -1; 					q.push(to); 				} 			} 		} 		inque[x]=false; 	} 	return dis[n]^dis[0]?dis[n]:-2; }
  int main() { 	clear(dis, 63); n=read(); ML=read(); DL=read(); 	for(RG int i=1;i<=ML;i++) 	{ 		a=read(); b=read(); c=read(); 		add_edge(a, b, c); 	} 	for(RG int i=1;i<=DL;i++) 	{ 		a=read(); b=read(); c=read(); 		add_edge(b, a, -c); 	} 	printf("%d\n", spfa()); 	return 0; }
   |