POJ 3169 Layout——差分约束
来源:互联网 发布:淘宝魔兽带团本 编辑:程序博客网 时间:2024/06/05 19:48
裸题,差分约束推荐这篇博客http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <queue>using namespace std;const int MAXN = 1010;const int MAXM = 200010;const int INF = 0x3f3f3f3f;bool vis[MAXN];int N, ML, MD, tot, head[MAXN], num[MAXN], dis[MAXN];struct Edge { int to, cost, next;}edge[MAXM];void Init() { tot = 0; for (int i = 1; i <= N; i++) head[i] = -1;}void AddEdge(int u, int v, int cost) { tot++; edge[tot].to = v; edge[tot].cost = cost; edge[tot].next = head[u]; head[u] = tot;}int spfa() { for (int i = 1; i <= N; i++) dis[i] = INF, vis[i] = false, num[i] = 0; dis[1] = 0, num[1]++; queue<int> q; q.push(1); while (!q.empty()) { int u = q.front(); q.pop(); vis[u] =false; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to, cost = edge[i].cost; if (dis[v] > dis[u] + cost) { dis[v] = dis[u] + cost; if (!vis[v]) { vis[v] = true; num[v]++; q.push(v); if (num[v] > N) { return -1; } } } } } return dis[N] == INF ? -2 : dis[N];}int main() { while (~scanf("%d %d %d", &N, &ML, &MD)) { Init(); for (int i = 1; i <= ML; i++) { int u, v, cost; scanf("%d %d %d", &u, &v, &cost); AddEdge(u, v, cost); } for (int i = 1; i <= MD; i++) { int u, v, cost; scanf("%d %d %d", &u, &v, &cost); AddEdge(v, u, -cost); } printf("%d\n", spfa()); } return 0;}
阅读全文
0 0
- 差分约束——Layout ( POJ 3169 )
- POJ 3169 Layout——差分约束
- POJ 3169 Layout(差分约束系统)
- POJ 3169 Layout [差分约束]
- POJ 3169 Layout 差分约束系统
- POJ 3169 Layout 差分约束系统
- poj 3169 Layout(差分约束)
- poj 3169 Layout(差分约束)
- poj 3169 Layout 差分约束
- POJ 3169 Layout(差分约束系统)
- POJ 3169 Layout (差分约束spfa)
- poj 3169 Layout (差分约束+Bellman )
- poj 3169 Layout (差分约束+Bellman )
- POJ 3169-Layout(差分约束系统)
- poj 3169 Layout 【差分约束】
- POJ 3169 Layout(差分约束啊)
- poj 3169 Layout 差分约束
- poj 3169 Layout (差分约束)
- 三栏布局:浮动布局、绝对布局、(相对)圣杯布局、双飞翼布局、中间固定两边自适应总结
- atitit 常见个人与企业与组织机构的信息化.docx
- java学习初探十三之集合—List详解
- Atititi atiitt eam pam资产管理 购物表去年.xlsx
- 牛客网-剑指offer-17-树的子结构
- POJ 3169 Layout——差分约束
- 数据安全 密码学原理与概论
- Atitit 身份证与银行卡校验规则
- Atitit uke消防防火规范 attilax总结
- 末段短才能突出主旨
- 牛客网-剑指offer-18-二叉树的镜像
- 通过修改别名,设置初步服务器的安全
- SQL视图的创建和使用
- 牛客网-剑指offer-19-顺时针打印矩阵