CodeVS 1242 布局
来源:互联网 发布:sql server创建表 编辑:程序博客网 时间:2024/05/23 01:39
//差分约束系统//以x-y <= k为例,等价于x <= k+y,相当于x和y之间的连边最大权值为k,然后跑最短路即知最大解。 //好久不做CodeVS了,洛谷大法好!#include<bits/stdc++.h>using namespace std;const int inf = 0x3f3f3f3f;queue<int> q;int n, m1, m2, tot;bool flag = 0, vis[1010];int dis[1010], st[1010], got[1010];struct node{ int v, w, nxt;} edge[20010];inline void in(int x, int y, int z){ edge[++tot].v = y; edge[tot].w = z; edge[tot].nxt = st[x]; st[x] = tot;}inline void getInit(){ memset(vis, 0, sizeof(0)); for(int i = 0; i <= n; i++) dis[i] = inf;}inline void GoSPFA(){ q.push(1); vis[1] = 1; dis[1] = 0; while(!q.empty()){ int now = q.front(); q.pop(); vis[now] = 0; for(int i = st[now]; i; i = edge[i].nxt){ int to = edge[i].v; if(dis[to] > dis[now] + edge[i].w){ dis[to] = dis[now] + edge[i].w; got[to]++; if(got[to] >= n){ flag = 1; return; } vis[to] = 1; q.push(to); } } }}int main(){ scanf("%d%d%d", &n, &m1, &m2); for(int i = 1, x, y, z; i <= m1; i++){ scanf("%d%d%d", &x, &y, &z); in(x, y, z); } for(int i = 1, x, y, z; i <= m2; i++){ scanf("%d%d%d", &x, &y, &z); in(y, x, -z); } getInit(); GoSPFA(); if(flag) printf("-1\n"); else if(dis[n] < inf) printf("%d\n", dis[n]); else printf("-2\n"); return 0;}
阅读全文
0 0
- codevs 1242 布局
- CodeVS 1242 布局
- 【CODEVS 1242】布局 SPFA + 差分约束
- Codevs 1242 布局 2005年USACO(差分约束)
- Codevs
- codevs 1060
- [CodeVS]3304
- codevs 4248
- Codevs 苹果树
- Codevs 挂缀
- Codevs 加密算法
- CodeVs 1011
- codevs 买票
- codevs 1257
- CodeVs 1009
- codevs 1011
- codevs 1038
- codevs 1044
- fedora kde桌面系统配置
- 关于设计
- Json解析精讲
- HDU 2665 (主席树)
- kde framework概述(KDE Framework译文)
- CodeVS 1242 布局
- liunx 配置jdk
- qt学习之sender
- Device Tree
- 修改commit的message
- arguments,callee和caller
- js 百度地图第二次加载显示不全问题解决方案
- 修改文件内容Java python
- 网络数据