社区讨论
WA求调
P3385【模板】负环参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @mhjumzlo
- 此快照首次捕获于
- 2025/11/04 08:47 4 个月前
- 此快照最后确认于
- 2025/11/04 08:47 4 个月前
CPP
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e3+5;
struct STU{
ll u,w;
}now;
ll dist[N],cnt[N];
bool p[N];
int main(){
freopen("P3385_3.in","r",stdin);
freopen("SB.out","r",stdout);
int T,n,m;
scanf("%d",&T);
while(T--){
vector<STU>v[2005];
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
now.u=b,now.w=c;
v[a].push_back(now);
if(c>=0){
now.u=a;
v[b].push_back(now);
}
}
memset(dist,0x3f,sizeof(dist));
memset(cnt,0,sizeof(cnt));
memset(p,0,sizeof(p));
queue<int>q;
q.push(1);
p[1]=1,dist[1]=0;
bool ok=0;
while(!q.empty()){
if(ok)break;
int t=q.front();
p[t]=0;
q.pop();
for(int i=0;i<v[t].size();i++){
int a=v[t][i].u,b=v[t][i].w;
if(dist[t]+b<dist[a]){
dist[a]=dist[t]+b;
if(!p[i]){
if(++cnt[a]>=n){
printf("YES\n");
ok=1;
break;
}
p[a]=1;
q.push(a);
}
}
}
}
if(!ok)printf("NO\n");
}
return 0;
}
https://www.luogu.com.cn/record/212535894
回复
共 0 条回复,欢迎继续交流。
正在加载回复...