Skip to content

Instantly share code, notes, and snippets.

@satellitex
Created August 13, 2014 01:21
Show Gist options
  • Save satellitex/247cee2a2b8e5c4a7daf to your computer and use it in GitHub Desktop.
Save satellitex/247cee2a2b8e5c4a7daf to your computer and use it in GitHub Desktop.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
#define INF (1LL<<50LL)
int N,M[2];
int A[10001][2],B[10001][2];
ll D[10001][2],d[1111];
ll solve(){
fill(d,d+1111,INF);
d[0]=0;
for(int k=0;k<N;k++){
for(int i=0;i<N-1;i++)
if( d[i+1] < INF )
d[i] = min(d[i],d[i+1]);
for(int j=0;j<2;j++){
for(int i=0;i<M[j];i++){
if( d[A[i][j]] < INF )
d[B[i][j]]= min( d[B[i][j]], d[A[i][j]] + D[i][j]);
}
}
}
if(d[0] < 0) {
return -1;
} else if( d[N-1]==INF ) {
return -2;
}
return d[N-1];
}
int main(){
cin >> N >> M[0] >> M[1];
int a,b,d;
for(int i=0;i<M[0];i++){
cin >> a >> b >> d;
--a; --b;
A[i][0] = a; B[i][0] = b; D[i][0] = d;
}
for(int i=0;i<M[1];i++){
cin >> a >> b >> d;
--a; --b;
A[i][1] = b; B[i][1] = a; D[i][1] = -d;
}
cout << solve() << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment