博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3169 Layout
阅读量:4625 次
发布时间:2019-06-09

本文共 1550 字,大约阅读时间需要 5 分钟。

裸差分约束。有两种写法。spfa 和 dfs,dfs 优点在判负环,但递归常数巨大,spfa 优点在快。

差分约束就是用最短路的思想来接不等式组,具体做法见。

#include 
using namespace std;#define db double#define ll long long#define RG registerinline int gi(){ RG int ret; RG bool flag; RG char ch; ret=0, flag=true, ch=getchar(); while (ch < '0' || ch > '9') ch == '-' ? flag=false : 0, ch=getchar(); while (ch >= '0' && ch <= '9') ret=(ret<<3)+(ret<<1)+ch-'0', ch=getchar(); return flag ? ret : -ret;}const db pi = acos(-1.0);const int N = 1005, M = 2e4+5, inf = 1<<30;bool vis[N];int f[N],dis[N],et[M],nx[M],ed[M];int num;inline void add(int x,int y,int z){ nx[++num]=f[x], et[num]=y, ed[num]=z, f[x]=num;}inline void spfa(int o){ int i,to,len; vis[o]=true; for (i=f[o]; i; i=nx[i]) { to=et[i], len=ed[i]; if (dis[to] > dis[o]+len) { if (vis[to]) puts("-1"), exit(0); dis[to]=dis[o]+len; spfa(to); } } vis[o]=false;}int main(){ freopen("layout.in","r",stdin); freopen("layout.out","w",stdout); int n,m,k,i,x,y,z; n=gi(), m=gi(), k=gi(); for (i=1; i<=m; ++i) { x=gi(), y=gi(), z=gi(); add(x,y,z); } for (i=1; i<=k; ++i) { x=gi(), y=gi(), z=gi(); add(y,x,-z); } for (i=2; i<=n; ++i) dis[i]=inf; spfa(1); dis[n] < inf ? printf("%d\n",dis[n]) : puts("-2"); return 0;}

转载于:https://www.cnblogs.com/y142857/p/7572955.html

你可能感兴趣的文章
Ubuntu 安装PostgreSQL
查看>>
数据可视化(6)--Google Charts实例
查看>>
hdu4339 Query
查看>>
关于Android 打开新的Activity 虚拟键盘的弹出与不弹出
查看>>
“万能数据库查询分析器”在四大软件下载网站的排行榜中均入围前10,可喜可贺...
查看>>
和菜鸟一起学linux总线驱动之smartcard操作模式和协议与参数选择
查看>>
android 开发工具(转)
查看>>
python中的uuid4
查看>>
CSS 必知的7个知识点
查看>>
asp.net mvc 生成条形码
查看>>
单调队列
查看>>
Attribute value is quoted with " which must be escaped when used within the value 问题解决
查看>>
作业01
查看>>
web学习记录-JS-12
查看>>
ubuntu安装软件包apt-get和dpkg方法
查看>>
像Hacker News一样排序博客园首页文章
查看>>
TFS数据服务器分析
查看>>
状态图(Statechart Diagram)
查看>>
find命令
查看>>
md5 对字符串进行加密的方法 简单好用
查看>>