10090. 「一本通 3.4 练习 2」布局 Layout
题意
有些奶牛是好基友,它们希望彼此之间的距离小于等于某个数。有些奶牛是情敌,它们希望彼此之间的距离大于等于某个数。
思路
如果两只奶牛是好基友,那么:
如果两只奶牛是情敌,那么:
即:
也就是:
直接上代码:
代码语言:javascript复制#include<algorithm>
#include<bitset>
#include<complex>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<cmath>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#define E 300010
#define eps 1e-10
#define ll long long
#pragma GCC optimize(2)
using namespace std;
inline ll read(){
ll res=0,f=1;char ch=getchar();
while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') res=res*10 ch-'0',ch=getchar();
return res*f;
}
inline void write(ll x){
if(x<0) putchar('-'),x=-x;
if(x<10) putchar(x '0');
else{
write(x/10);
putchar(x '0');
}
}
//queue<ll> q;
//set<ll> s;
//priority_queue<ll> q1;
//priority_queue<ll,vector<ll>,greater<ll> > q2;
//list<ll> l;
//stack<ll> s;
ll n,k;
ll fir[E],nxt[E],son[E],w[E],tot,inf,ans;
inline void add(register ll x,register ll y,register ll z){
tot;
w[tot]=z;
nxt[tot]=fir[x];
fir[x]=tot;
son[tot]=y;
}
ll dis[E],vis[E],tt[E];
deque<ll> q;
inline void spfa(int s){
memset(dis,63,sizeof(dis));
memset(tt,0,sizeof(tt));
memset(vis,0,sizeof(vis));inf=dis[0];
dis[s]=0;vis[s]=1;
while(!q.empty()) q.pop_front();
q.push_back(s);
while(!q.empty()){
register ll u=q.front();q.pop_front();
vis[u]=0;
for(register ll i=fir[u];i;i=nxt[i]){
register ll to=son[i];
if(dis[to]>dis[u] w[i]){
tt[to] ;
dis[to]=dis[u] w[i];
if(tt[to]>n 1){
puts("-1");
exit(0);
}
if(!vis[to]){
vis[to]=1;
if(!q.empty()&&dis[to]<dis[q.front()]) q.push_front(to);
else q.push_back(to);
}
}
}
}
}
int k1,k2;
int main(){
n=read();k1=read();k2=read();
for(register ll i=1;i<=k1;i ){
ll x=read(),y=read(),z=read();
add(x,y,z);
}
for(register ll i=1;i<=k2;i ){
ll x=read(),y=read(),z=read();
add(y,x,-z);
}
spfa(1);
ans=dis[n];
for(int i=1;i<=n;i ) spfa(i);
if(ans>=inf) puts("-2");
else write(ans),putchar('n');
return 0;
}