(已经有了简化版题面)

又秒了一次dp233

本来按照感觉瞎写了一发...

但还是老老实实列式子吧....

对差分约束有了更深的理解

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
/*
不等式*3
设前i头奶牛有斑点牛d[]个,设lr为给定区间
则:
1、每个区间有且只有一头奶牛有斑点=>d[r]-d[l-1]=1; 
2、两只相邻的奶牛要么有斑点要么没斑点=>0<=d[i]-d[i-1]<=1;
改写方程得:

d[r]-d[l-1]<=1
d[l-1]-d[r]<=-1
d[i]-d[i-1]<=1
d[i-1]-d[i]<=0

于是:

l-1->r 1
r->l-1 -1
i-1->i 1
i->i-1 0

然后流氓优化+负环spfa卡过了这道题
*/
const int maxn=1e6+;
int n,m;
inline int read()
{
int x=,f=;char s=getchar();
while(s>''||s<''){if(s=='-')f=-;s=getchar();}
while(s<=''&&s>=''){x=x*+s-'';s=getchar();}
return x*f;
}
struct edge
{
int to,next,dis;
}e[maxn];
int head[maxn],cnt;
inline void addedge(int from,int to,int dis)
{
e[++cnt].next=head[from];
e[cnt].to=to;
e[cnt].dis=dis;
head[from]=cnt;
}
int dis[maxn],vis[maxn];
struct cmp
{
bool operator()(int a,int b)
{
return dis[a]>dis[b];
}
};
//queue < int > q;
int tot=;
priority_queue < int , vector < int > , cmp > q;
inline void spfa(int s)
{
/*for(int i=1;i<=n;i++)
{
dis[i]=2147483647;
}*/
memset(dis,0x7f,sizeof(dis));
q.push(s);
vis[s]=;
dis[s]=;
while(!q.empty())
{
int u=q.top();
q.pop();
vis[u]=;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].to;
if(dis[v]>dis[u]+e[i].dis)
{
dis[v]=dis[u]+e[i].dis;
if(vis[v]==)
{
if(++tot>)
{
printf("-1");
exit();
}
q.push(v);
vis[v]=;
}
}
}
}
} int main()
{
n=read();m=read();//scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int x,y;
x=read();y=read();//scanf("%d%d",&x,&y);
addedge(x-,y,);
addedge(y,x-,-);
}
for(int i=;i<=n;i++)
{
addedge(i,i-,);
addedge(i-,i,);
}
spfa();
printf("%d",dis[n]);
return ;
}

P3084 [USACO13OPEN]照片(差分约束)的更多相关文章

  1. 洛谷 P3084 [USACO13OPEN]照片Photo 解题报告

    [USACO13OPEN]照片Photo 题目描述 农夫约翰决定给站在一条线上的\(N(1 \le N \le 200,000)\)头奶牛制作一张全家福照片,\(N\)头奶牛编号\(1\)到\(N\) ...

  2. P3084 [USACO13OPEN]照片Photo

    题目描述 农夫约翰决定给站在一条线上的N(1 <= N <= 200,000)头奶牛制作一张全家福照片,N头奶牛编号1到N. 于是约翰拍摄了M(1 <= M <= 100,00 ...

  3. P3084 [USACO13OPEN]照片Photo (dp+单调队列优化)

    题目链接:传送门 题目: 题目描述 Farmer John has decided to assemble a panoramic photo of a lineup of his N cows ( ...

  4. P3084 [USACO13OPEN]照片Photo dp

    题意: 有n个区间,每个区间只能有一个斑点奶牛,问最多有几个斑点奶牛. 思路: 首先要处理出每个点的L[i],R[i]. L[i]表示L[i]-i-1之间一定有一个点.i也是选中的. R[i]表示R[ ...

  5. 洛谷P3084 [USACO13OPEN]照片

    题目 \(DP\) 设状态\(dp[i]\)为\(i\)位置放了斑点牛,前\(i\)个位置能得到的最多的牛. 有方程\(dp[i]=max(dp[j]+1,dp[i])\),而我们并不知道什么\(j\ ...

  6. Candies-POJ3159差分约束

    Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...

  7. poj3159 差分约束 spfa

    //Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...

  8. ZOJ 2770火烧连营——差分约束

    偶尔做了一下差分约束. 题目大意:给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵. ---------------------------------- ...

  9. POJ 2983 Is the Information Reliable? 差分约束

    裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...

随机推荐

  1. springmvc中将servlet api对象作为处理方法的入参使用

    在springmvc中,控制器不依赖任何servlet api对象,也可以将servlet api对象作为处理方法的入参使用,非常方便,比如需要使用HttpSession对象,那么就可以直接将Http ...

  2. Chrome 和 Chromedriver 的安装和配置

    1.Chrome 下载安装地址:https://www.google.com/intl/zh-CN_ALL/chrome/ 2.Chromedriver的安装 方法一: (1) brew cask i ...

  3. NoUniqueBeanDefinitionException常见异常!!

    Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of ...

  4. 车载导航应用中基于Sketch UI主题定制方案的实现

    1.导读 关于应用的主题定制,相信大家或多或少都有接触,基本上,实现思路可以分为两类: 内置主题(应用内自定义style) 外部加载方式(资源apk形式.压缩资源.插件等) 其实,针对不同的主题定制实 ...

  5. python中函数调用---可变对象以及不可变对象

    # 定义函数 def demo(obj): print("原值: ",obj) obj += obj #调用函数 print("========值传递=======&qu ...

  6. 如何让excel文件读取变得更简单

    今天给大家安利一款excel文件导入神器,easyexcel,官方地址:(https://github.com/alibaba/easyexcel). 在官网文档中有介绍了其性能. 从上面的性能测试可 ...

  7. MySQL 拿 WebShell

    两种常规方法利用 MySQL getshell 的方法: select … into outfile general_log 一.select … into outfile 介绍 利用需要满足以下条件 ...

  8. 记一次EF Core DBContext在Action委托中GC异常的问题.

    今天在开发过程中发现.在SaveChanges的时候偶尔会抛出异常:Cannot access a disposed object. A common cause of this error is d ...

  9. CentOS7使用‘中科大源’

    中科大的源质量速度都不错,推荐使用. 这里列出CentOS 7的Base和epel的源. 进入/etc/yum.repos.d/中,将原本的几个repo文件备份,之后新建三个repo文件 内容如下: ...

  10. 利用ansible书写playbook在华为云上批量配置管理工具自动化安装ceph集群

    首先在华为云上购买搭建ceph集群所需云主机: 然后购买ceph所需存储磁盘 将购买的磁盘挂载到用来搭建ceph的云主机上 在跳板机上安装ansible 查看ansible版本,检验ansible是否 ...