题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2330

差分约束,再建立一个源点0,向所有点连边权为1的边,表示每个人都会分到糖果;

答案较大,需要开long long;

据说有个大数据会T,所以需要0点从n向1连边;

WA了数次,竟然是没看清条件...不少于,不多于什么的...

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
queue<int>q;
int const MAXN=1e5+;
int n,k,head[MAXN],ct,dis[MAXN],cnt[MAXN];
bool in[MAXN];
long long ans;
struct N{
int to,next,w;
N(int t=,int n=,int w=):to(t),next(n),w(w) {}
}edge[MAXN<<];
bool spfa(int s)
{
memset(in,,sizeof in);
memset(dis,-,sizeof dis);
dis[s]=;in[s]=;q.push(s);cnt[s]=;
while(q.size())
{
int x=q.front();q.pop();in[x]=;
for(int i=head[x],u;i;i=edge[i].next)
{
if(dis[u=edge[i].to]<dis[x]+edge[i].w)
{
if(++cnt[u]>=n)return ;
dis[u]=dis[x]+edge[i].w;
if(!in[u])in[u]=,q.push(u);
}
}
}
return ;
}
int main()
{
scanf("%d%d",&n,&k);
int tp,x,y;
for(int i=;i<=k;i++)
{
scanf("%d%d%d",&tp,&x,&y);
if(tp==){edge[++ct]=N(y,head[x],),head[x]=ct;
edge[++ct]=N(x,head[y],),head[y]=ct;}
if(tp==){if(x==y){printf("-1");return ;}
else edge[++ct]=N(y,head[x],),head[x]=ct;}
if(tp==)edge[++ct]=N(x,head[y],),head[y]=ct;
if(tp==){if(x==y){printf("-1");return ;}
else edge[++ct]=N(x,head[y],),head[y]=ct;}
if(tp==)edge[++ct]=N(y,head[x],),head[x]=ct;
}
for(int i=n;i;i--)
edge[++ct]=N(i,head[],),head[]=ct;
if(!spfa())
{
printf("-1");return ;
}
for(int i=;i<=n;i++)ans+=dis[i];
printf("%lld",ans);
return ;
}

最新文章

  1. centos下建立双机信任关系
  2. spring hibernate4 c3p0连接池配置
  3. [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索
  4. linux 下更改 blast+ version
  5. 在C#中对Datatable排序【DefaultView的Sort方法】
  6. ios 5
  7. WINAPI 变量(2861个)
  8. vsftpd.conf配置详解
  9. 转:ORACLEERP开发基础之EBS开发基础
  10. Node.js权威指南 (9) - 进程与子进程
  11. pl sql developer登陆界面找不到oracle数据库选项
  12. SLC和MLC
  13. 笔记-linux下Qt5.3.2 静态编译
  14. lintcode.22 平面列表
  15. hdu1356&amp;hdu1944 博弈论的SG值(王道)
  16. ssm+maven多模块项目整合
  17. SQL 收缩数据库日志的几种办法 (2005与2008 略有区别)
  18. [转帖]5G网速那么快,基站辐射会很大吗?
  19. springMVC怎么接受前台传过来的多种类型参数?(集合、实体、单个参数)
  20. jmap获取内存排名靠前的对象

热门文章

  1. Six ways to think like a journalist!
  2. AngularJS的ng-repeat显示表格
  3. PS 抠图如何使用通道法处理头发
  4. mysql: 关于MySQL InnoDB锁行还是锁表?
  5. D堆的实现
  6. UVA1422-Processor(二分法+优先队列)
  7. 《MySQL必知必会学习笔记》:子查询
  8. [省选]板块(shenben已经AFO!!!)
  9. 【BZOJ2521】[Shoi2010]最小生成树 最小割
  10. eacharts 根据后台数据生成柱状图