POJ 1932 XYZZY (差分约束+最长路)
来源:互联网 发布:域名续费多少钱 编辑:程序博客网 时间:2024/06/05 09:37
解题思路:按照题目给定条件建图,判断能不能找到一条路到n,在这条路上权值和不会为负数,初始值为100.
只需要用spfa跑最长路,遇到正环的时候将当前节点到起点的距离置为 inf 即可。这样可以保证有正环的图一定可以得到解。
这道题的代码需要注意一些细节和剪枝。
AC代码:
/** @Author: wchhlbt* @Last Modified time: 2017-09-20*/#include <vector>#include <list>#include <map>#include <set>#include <queue>#include <stack>#include <bitset>#include <algorithm>#include <functional>#include <numeric>#include <utility>#include <sstream>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <ctime>#include <cstring>#include <limits>#include <climits>#include <cstdio>#define Fori(x) for(int i=0;i<x;i++)#define Forj(x) for(int j=0;j<x;j++)#define maxn 107#define inf 0x3f3f3f3f#define ONES(x) __builtin_popcount(x)#define pb push_back#define AA first#define BB second#define _ << " " <<using namespace std;typedef long long ll ;const double eps =1e-8;const int mod = 998244353;const double PI = acos(-1.0);int dx[5] = {0,0,1,-1,0};int dy[5] = {1,-1,0,0,0};inline int read(){ int cnt; scanf("%d",&cnt); return cnt;}//每次使用前需要调用init函数初始化 可以处理负权边//最坏复杂度O(V*E)int d[maxn],inq[maxn];//inq数组储存当前点是否在队列中int deg[maxn];//记录每个节点入队次数vector<int> e[maxn]; //pair<节点, 边权> int val[maxn];int n,m;void init(){ for(int i = 0; i<maxn; i++){ e[i].clear(); d[i] = -inf; inq[i] = 0; deg[i] = 0; }}void SPFA(int s)//s为起点{ queue<int> Q; Q.push(s); d[s] = 100; inq[s] = 1; deg[s] = 1; while(!Q.empty()){ int hd = Q.front(); Q.pop(); inq[hd] = 0; for(int i = 0; i<e[hd].size(); i++){ int u = e[hd][i]; int v = val[u]; if(d[u]<d[hd]+v && d[hd]+v>0){//注意这里的判断条件 d[u] = d[hd] + v; if(inq[u]==1 || deg[u]>n) continue;//注意这个地方的剪枝 inq[u] = 1; deg[u]++; if(deg[u]>n) d[u] = inf; Q.push(u); } } } if(d[n]>0)puts("winnable"); elseputs("hopeless");}int main(){ while(~scanf("%d",&n)) { if(n==-1)break; init(); for(int i = 1; i<=n; i++){ val[i] = read(); int num = read(); for(int j = 1; j<=num; j++){ int k = read(); e[i].pb(k); } } SPFA(1); } return 0;}
阅读全文
0 0
- POJ 1932 XYZZY (差分约束+最长路)
- POJ 1201 差分约束、最长路
- poj 1201(差分约束+最长路)
- poj 1201 Intervals(差分约束求最长路)
- poj 1201 Intervals(差分约束,最长路)
- POJ 1932 XYZZY (spfa判断正环+判断是否连通 or SPFA+floyd 求解差分约束)
- poj 1716(最长路+差分约束系统)
- poj 1752 Advertisement (区间差分约束+最长路 输出可行解)
- POJ1201 差分约束最长路
- poj 1364 King 【非连通图的差分约束】【最长路 + 最短路求法】
- poj 1275 Cashier Employment 【差分约束】【经典建模】 【最短路求法 + 最长路求法】
- POJ 1201 & HDU1384 & ZOJ 1508 Intervals(差分约束+spfa 求最长路径)
- poj 1201 Intervals(差分约束)
- POJ 3195 Candies (差分约束)
- poj 2983(差分约束)
- POJ 1716 (差分约束)
- POJ 1201 (差分约束)
- POJ 1201 Intervals(差分约束)
- C语言 两个矩形的合并
- chrome浏览器对http和https网站安全提示和证书导入导出
- 关于GitHub
- 麦克纳姆轮及其速度分解计算
- Tomcat 服务器 Session的实现
- POJ 1932 XYZZY (差分约束+最长路)
- win10下安装并测试apache服务器
- [LeetCode][94,144,145]Binary Tree (Pre/In/Post)order Traversal
- 单片机控制TFT液晶屏显示数字
- Linux下记录所有用户操作的脚本
- python3读取解析邮件内容
- UVa 10118 Free Candies 记忆化搜索
- 网络应用的需求与传输层服务
- scalikeJDBC