hdu 3666(差分约束,手动栈解决超时问题)
THE MATRIX PROBLEM
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8016 Accepted Submission(s): 2092
is positive and no more than 1000, The problem is that if there exist N
numbers a1, a2, … an and M numbers b1, b2, …, bm, which satisfies that
each elements in row-i multiplied with ai and each elements in column-j
divided by bj, after this operation every element in this matrix is
between L and U, L indicates the lowerbound and U indicates the
upperbound of these elements.
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.
2 3 4
8 2 6
5 2 9
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<math.h>
using namespace std;
const double INF = ;
const int N = ;
const int M = ;
struct Edge{
int v,next;
double w;
}edge[M];
int head[N],tot;
int n,m,l,r;
void init(){
memset(head,-,sizeof(head));
tot = ;
}
void addEdge(int u,int v,double w,int &k){
edge[k].v =v ,edge[k].w = w ,edge[k].next = head[u],head[u] = k++;
}
double low[N];
int time[N];
bool vis[N];
bool spfa(int s){
for(int i=;i<=n+m;i++){
vis[i] = false;
time[i] = ;
low[i] = INF;
}
int num = ((int)sqrt(n+m))+;
low[s] = ;
time[s]++;
queue<int>q;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
double w = edge[k].w;
if(low[v]>low[u]+w){
low[v] = low[u]+w;
if(!vis[v]){
vis[v] = true;
q.push(v);
time[v]++;
if(time[v]>num) return false;
}
}
}
}
return true;
}
int main()
{
double c;
while(scanf("%d%d%d%d",&n,&m,&l,&r)!=EOF){
init();
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf("%lf",&c);
addEdge(i,n+j,-log(l/c),tot);
addEdge(n+j,i,log(r/c),tot);
}
}
for(int i=;i<=n+m;i++){
addEdge(,i,,tot);
}
if(spfa()) printf("YES\n");
else printf("NO\n");
}
return ;
}
手动栈解决:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<math.h>
using namespace std;
const double INF = ;
const int N = ;
const int M = ;
struct Edge{
int v,next;
double w;
}edge[M];
int head[N],tot;
int n,m,l,r;
void init(){
memset(head,-,sizeof(head));
tot = ;
}
void addEdge(int u,int v,double w,int &k){
edge[k].v =v ,edge[k].w = w ,edge[k].next = head[u],head[u] = k++;
}
double low[N];
int time[N];
bool vis[N];
int stk[N*N];
bool spfa(int s){
for(int i=;i<=n+m;i++){
vis[i] = false;
time[i] = ;
low[i] = INF;
}
int top = ;
low[s] = ;
time[s]++;
stk[top++] = s;
while(top!=){
int u = stk[--top];
vis[u] = false;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
double w = edge[k].w;
if(low[v]>low[u]+w){
low[v] = low[u]+w;
if(!vis[v]){
vis[v] = true;
stk[top++] = v;
time[v]++;
if(time[v]>n+m) return false;
}
}
}
}
return true;
}
int main()
{
double c;
while(scanf("%d%d%d%d",&n,&m,&l,&r)!=EOF){
init();
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf("%lf",&c);
addEdge(i,n+j,-log(l/c),tot);
addEdge(n+j,i,log(r/c),tot);
}
}
for(int i=;i<=n+m;i++){
addEdge(,i,,tot);
}
if(spfa()) printf("YES\n");
else printf("NO\n");
}
return ;
}
hdu 3666(差分约束,手动栈解决超时问题)的更多相关文章
- hdu 1531(差分约束)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1531 差分约束的题之前也碰到过,刚好最近正在进行图论专题的训练,就拿来做一做. ①:对于差分不等式,a ...
- I - 动物狂想曲 HDU - 6252(差分约束)
I - 动物狂想曲 HDU - 6252 雷格西桑和路易桑是好朋友,在同一家公司工作.他们总是一起乘地铁去上班.他们的路线上有N个地铁站,编号从1到N.1站是他们的家,N站是公司. 有一天,雷格西桑起 ...
- hdu 4598 差分约束
思路:首先就是判断是否有奇环,若存在奇环,则输出No. 然后用差分约束找是否符合条件. 对于e(i,j)属于E,并且假设顶点v[i]为正数,那么v[i]-v[j]>=T--->v[j]-v ...
- hdu 1364(差分约束)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12056 Accepted: 4397 Description ...
- hdu 1534(差分约束)
Schedule Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1534(差分约束+spfa求最长路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1534 思路:设s[i]表示工作i的开始时间,v[i]表示需要工作的时间,则完成时间为s[i]+v[i] ...
- hdu 3440 差分约束
看完题目第一遍,感觉很简单.当写完程序跑测试用例的时候,发现第二个总是过不了,然后好好研究了一下测试用例,才知道原来不是程序有问题,而是我的建图方式错了.对于这些无序的点,如果高的在右边,不等式是di ...
- hdu 3440(差分约束好题)
House Man Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- POJ 3159 Candies 还是差分约束(栈的SPFA)
http://poj.org/problem?id=3159 题目大意: n个小朋友分糖果,你要满足他们的要求(a b x 意思为b不能超过a x个糖果)并且编号1和n的糖果差距要最大. 思路: 嗯, ...
随机推荐
- spring MVC 字符串数组传值 字符带有逗号,问题
按照如下图所示方式传值,想在后台得到一个长度为1的数组,后台直接根据,进行分割,就得到长度为2的数组 1.曲线救国解决法 解决方案, 前端对参数进行编码 encodeURIComponent(valu ...
- 使用 TListView 控件(2)
本例效果图: 代码文件: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, ...
- 【bzoj4052】[Cerc2013]Magical GCD 暴力
题目描述 给出一个长度在 100 000 以内的正整数序列,大小不超过 10^12. 求一个连续子序列,使得在所有的连续子序列中,它们的GCD值乘以它们的长度最大. 样例输入 1 5 30 60 2 ...
- Jprofiler分析WebSphere(配置WebSphereagent代理)
一. Windows+WebSphere+Jprofiler9 我们自己的windows机器监控本地的WebSphere,应该为服务器配置监控代理,供Jprofiler连接使用,具体步骤如下: 1 ...
- media="screen"是什么意思?
<link rel="stylesheet" href="css/main.css" type="text/css" media=&q ...
- ZJOI2018 Day2 滚粗记 + 流水账
一脸懵逼地就被直接拉过来浙江省选了,一年参加两次省选成就达成-- 讲课啥的都没听,过去休息了一天就进行比赛了.考试之前感冒没好透,精神不是 \(100\%\) 的状态,但是并无大碍(反正最后都很凉). ...
- 线程--promise furture 同步
http://www.cnblogs.com/haippy/p/3279565.html std::promise 类介绍 promise 对象可以保存某一类型 T 的值,该值可被 future 对象 ...
- Consumer [分组背包]
Consumer Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others) Total Subm ...
- [lucene系列笔记2]在eclipse里初步使用lucene的索引和查询功能
首先,new一个java project,名字叫做LuceneTools. 然后,在project里new一个class,名字叫做IndexFiles.这个类用来给文件建索引(建好索引以后就可以高效检 ...
- BS架构下使用消息队列的工作流程
异步通信 对于BS(Browser-Server 浏览器)架构,很多情景下server的处理时间较长. 如果浏览器发送请求后,保持跟server的连接,等待server响应,那么一方面会对用户的体验有 ...