社区讨论
80求调
P3275[SCOI2011] 糖果参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @mli2b2dc
- 此快照首次捕获于
- 2026/02/11 21:25 4 周前
- 此快照最后确认于
- 2026/02/11 22:25 4 周前
80分求条QwQ,orz
CPP#include<bits/stdc++.h>
using namespace std;
struct node{
int v,w;
};
const int N=1e5+10;
vector<node> g[N];
int n,m;
int dis[N],vis[N],cnt[N];
queue<int> q;
bool spfa(int s){
memset(dis,-1,sizeof dis);
dis[s]=0,vis[s]=1,q.push(s);
while(!q.empty()){
int u=q.front();q.pop();vis[u]=0;
for(auto c:g[u]){
int v=c.v,len=c.w;
if(dis[u]+len>dis[v]){
dis[v]=dis[u]+len;
cnt[v]=cnt[u]+1;
if(cnt[v]>=n+1) return true;
if(vis[v]==0) vis[v]=1,q.push(v);
}
}
}
return false;
}
signed main()
{
// ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>m;
// if(n==4&&m==7){
// cout<<8;
// return 0;
// }
int opt,x,y;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&opt,&x,&y);
if(opt==1){
g[x].push_back({y,0});
g[y].push_back({x,0});
}
if(opt==2){
g[x].push_back({y,1});
// g[y].push_back({x,1});
}
if(opt==3){
g[y].push_back({x,0});
// g[x].push_back({y,0});
}
if(opt==4){
g[y].push_back({x,1});
// g[x].push_back({y,1});
}
if(opt==5){
g[x].push_back({y,0});
// g[y].push_back({x,0});
}
}
for(int i=1;i<=n;i++){
g[0].push_back({i,1});
}
if(spfa(0)){
cout<<-1;
}
else{
int ans=0;
for(int i=1;i<=n;i++){
ans+=dis[i];
}
cout<<ans;
}
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...