布局
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<vector> 8 #include<map> 9 #include<set> 10 #include<stack> 11 using namespace std; 12 int read(){ 13 int x=0,f=1;char s=getchar(); 14 while(s<'0'||s>'9'){if(s=='-') f=-1;s=getchar();} 15 while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();} 16 return x*f; 17 } 18 queue<int>q; 19 const int maxn=1007; 20 const int maxm=20007; 21 const int INF=0x7f7f7f7f; 22 int n,ml,md,num; 23 int head[maxn],cnt[maxn],d[maxn]; 24 bool flag; 25 bool inq[maxn]; 26 struct Edge{ 27 int nxt,to,dis; 28 }edge[maxm<<1]; 29 void add(int from,int to,int dis){ 30 edge[++num].nxt=head[from]; 31 edge[num].to=to; 32 edge[num].dis=dis; 33 head[from]=num; 34 } 35 void spfa(int x){ 36 memset(inq,false,sizeof(inq)); 37 memset(d,INF,sizeof(d)); 38 memset(cnt,0,sizeof(cnt)); 39 d[x]=0;inq[x]=true;q.push(x); 40 while(!q.empty()){ 41 int u=q.front();inq[u]=false;q.pop(); 42 for(int i=head[u];i;i=edge[i].nxt){ 43 int v=edge[i].to; 44 if(d[v]>d[u]+edge[i].dis){ 45 d[v]=d[u]+edge[i].dis; 46 if(!inq[v]){ 47 cnt[v]++;inq[v]=true;q.push(v); 48 if(cnt[v]>n){ 49 cout<<-1<<endl;flag=true;return; 50 } 51 } 52 } 53 } 54 } 55 } 56 int main(){ 57 //freopen("a.in","r",stdin); 58 n=read();ml=read();md=read(); 59 for(int i=1;i<=ml;i++){ 60 int u,v,w;u=read();v=read();w=read(); 61 add(u,v,w); 62 } 63 for(int i=1;i<=md;i++){ 64 int u,v,w;u=read();v=read();w=read(); 65 add(v,u,-w); 66 } 67 for(int i=1;i<=n;i++) add(0,i,0); 68 for(int i=2;i<=n;i++) add(i,i-1,0); 69 spfa(0); 70 if(!flag){ 71 spfa(1); 72 if(d[n]==INF) cout<<-2<<endl; 73 else cout<<d[n]<<endl; 74 } 75 return 0; 76 }