本蒟蒻提交程序后成功获得了 42 分的好成绩:
于是下载了本题第三组测试数据:
但测试后本蒟蒻的输出好像没有问题,有人能帮本蒟蒻解释一下原因吗?
以下是本蒟蒻的代码(思路:差分约束):
#include<bits/stdc++.h>
using namespace std;
const int N=2e3+5;
const int M=2e4+5;
const int INF=1e9+7;
int n,ml,md;
queue<int>q;
bitset<N>inq;
int dis[N],cnt[N];
int head[N],k;
struct edge{
int v,w;
int nxt;
}e[N+M];
inline void add_edge(int u,int v,int w){
++k;
e[k].v=v;
e[k].w=w;
e[k].nxt=head[u];
head[u]=k;
}
inline int read(){
int res=0,f=1;
char ch=getchar();
while(ch>'9'||ch<'0'){
if(ch=='-') f=-1;
ch=getchar();
}while(ch<='9'&&ch>='0'){
res=(res<<3)+(res<<1)+(ch^48);
ch=getchar();
}return res*f;
}
bool spfa(int s){
for(int i=1;i<=n;++i)
dis[i]=INF;
q.push(s);
inq[s]=1;
dis[s]=0;
while(q.size()){
int u,v,w;
u=q.front();
q.pop();
inq[u]=0;
for(int i=head[u];i;i=e[i].nxt){
v=e[i].v;w=e[i].w;
if(dis[v]>dis[u]+w){
dis[v]=dis[u]+w;
cnt[v]=cnt[u]+1;
if(cnt[v]>=n&&s==0) return false;
if(!inq[v]){
q.push(v);
inq[v]=1;
}
}
}
}return true;
}
int main(){
// freopen("qwq.in","r",stdin);
n=read();ml=read();md=read();
int u,v,w;
for(int i=1;i<=n;++i){
add_edge(0,i,0);
if(i!=n) add_edge(u+1,u,0);
}
while(ml--){
u=read();v=read();w=read();
add_edge(u,v,w);
}while(md--){
u=read();v=read();w=read();
add_edge(v,u,-w);
}
if(!spfa(0)) cout<<"-1";
else{
spfa(1);
if(dis[n]>1e9) cout<<"-2";
else cout<<dis[n];
}
return 0;
}