HDU3666 差分约束
THE MATRIX PROBLEM
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8418 Accepted Submission(s): 2179
Each case includes two parts, in part 1, there are four integers in one line, N,M,L,U, indicating the matrix has N rows and M columns, L is the lowerbound and U is the upperbound (1<=N、M<=400,1<=L<=U<=10000). In part 2, there are N lines, each line includes M integers, and they are the elements of the matrix.
//显然不满足差分约束的条件,可以L<=cij*ai/bj<=R两边除cij(cij>0)后取对数得到
//log(L/cij)<=log(ai)-log(bj)<=log(R/cij).只求存不存在就行。但是本体如果用stl
//的queue写spfa会超时(可以用节点出队次数小于sqrt(n)判断),可以自己定义一个栈来存储。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=;
const double inf=;
int n,m,tol,head[maxn*+],cnt[maxn*+],stk[maxn*maxn];
double L,R,dis[maxn*+];
bool mark[maxn*+];
struct node
{
int to,next;
double val;
}nodes[];
void Add(int a,int b,double c)
{
nodes[tol].to=b;
nodes[tol].val=c;
nodes[tol].next=head[a];
head[a]=tol++;
}
bool spfa(int s)
{
for(int i=;i<=n+m;i++){
dis[i]=inf;
cnt[i]=;
mark[i]=;
}
int top=;
stk[++top]=s;
mark[s]=;cnt[s]++;dis[s]=;
while(top>){
int u=stk[top--];
mark[u]=;
for(int i=head[u];i!=-;i=nodes[i].next){
int v=nodes[i].to;
if(dis[v]>dis[u]+nodes[i].val){
dis[v]=dis[u]+nodes[i].val;
if(!mark[v]){
mark[v]=;
if(++cnt[v]>=n+m) return ;
stk[++top]=v;
}
}
}
}
return ;
}
int main()
{
while(scanf("%d%d%lf%lf",&n,&m,&L,&R)==){
tol=;
memset(head,-,sizeof(head));
double x;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf("%lf",&x);
Add(j+n,i,log(R/x));
Add(i,j+n,-log(L/x));
}
}
for(int i=;i<=n+m;i++)//加一个公共源点0
Add(,i,);
if(spfa()) printf("YES\n");
else printf("NO\n");
}
return ;
}
HDU3666 差分约束的更多相关文章
- HDU3666 THE MATRIX PROBLEM (差分约束+取对数去系数)(对退出情况存疑)
You have been given a matrix C N*M, each element E of C N*M is positive and no more than 1000, The p ...
- Candies-POJ3159差分约束
Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...
- poj3159 差分约束 spfa
//Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...
- ZOJ 2770火烧连营——差分约束
偶尔做了一下差分约束. 题目大意:给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵. ---------------------------------- ...
- POJ 2983 Is the Information Reliable? 差分约束
裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...
- 2014 Super Training #6 B Launching the Spacecraft --差分约束
原题:ZOJ 3668 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3668 典型差分约束题. 将sum[0] ~ sum ...
- POJ 1364 King --差分约束第一题
题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...
- [USACO2005][POJ3169]Layout(差分约束)
题目:http://poj.org/problem?id=3169 题意:给你一组不等式了,求满足的最小解 分析: 裸裸的差分约束. 总结一下差分约束: 1.“求最大值”:写成"<=& ...
- ShortestPath:Layout(POJ 3169)(差分约束的应用)
布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...
随机推荐
- java扫描控制台输入
由于因最近练习算法的需要,加上API文档中翻译的太过模糊,做了一些小测试,算是武断的记下一些个人结论. Scanner cin = new Scanner(System.in); 对于cin.next ...
- LeetCode - 442. Find All Duplicates in an Array - 几种不同思路 - (C++)
题目 题目链接 Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ...
- usdt信息小结
https://blog.csdn.net/weixin_42208011/article/details/80499536 https://blog.csdn.net/weixin_42208011 ...
- /etc/fstab 文件如何填写(转)
转载自 http://hi.baidu.com/jingzhongchen/blog/item/8e6f552dcead7ce98b139952.html 看你对/etc/fstab文件了解多少? ...
- 图的遍历——BFS(队列实现)
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> ...
- "Hello world!"团队第一次会议
今天是我们"Hello world!"团队第一次召开会议,今天的会议可能没有那么正式,但是我们一起确立了选题——基于WEB的售票系统.博客内容是: 1.会议时间 2.会议成员 3. ...
- 20145214 《Java程序设计》第4周学习总结
20145214 <Java程序设计>第4周学习总结 教材学习内容总结 继承 继承基本上就是避免多个类间重复定义共同行为.要避免在程序设计上出现重复,可以把相同的程序代码提升为父类. 关键 ...
- j2ee—框架(2):Servlet+JSP实现基本的登录功能(v2.0)
该部分将逻辑判断在UserBean中进行处理,而且不采用配置的方式去实现,为了区分开两种实现方法的不同,在这里将之前设置的内容只是备注掉,并不会删除,也方便之后将两种方式进行对比. 第一部分 Logi ...
- 【Docker 命令】- build命令
docker build 命令用于使用 Dockerfile 创建镜像. 语法 docker build [OPTIONS] PATH | URL | - OPTIONS说明: --build-arg ...
- vue 开发多页应用
vue 开发多页应用 https://www.cnblogs.com/fengyuqing/p/vue_cli_webpack.html https://segmentfault.com/a/1190 ...