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的糖果差距要最大. 思路: 嗯, ...
随机推荐
- 本周学习总结JAVA
6. 为如下代码加上异常处理 byte[] content = null; FileInputStream fis = new FileInputStream("testfis.txt&qu ...
- Storm之详解spout、blot
1.Topology的构造backtype.storm.topology.TopologyBuilder 2.Spout组件的编写实现接口 backtype.storm.topology.IRichS ...
- [Java文件操作] 将素数输出到文件
[要求]编写程序求出10万以内的所有素数,并将这些素数输出到一个文本文件中,每行文本只包含一个素数数据. import java.util.*; import java.io.*; public cl ...
- DataTable定义
DataTable是一个临时保存数据的网格虚拟表(表示内存中数据的一个表.).DataTable是ADO dot net 库中的核心对象.它可以被应用在 VB 和 ASP 上.它无须代码就可以简单的绑 ...
- [洛谷P1131][ZJOI2007]时态同步
题目大意:给你一棵树,每条边有边权,要求增加一些边的边权,使得根节点到每个叶子节点的距离相等,求出最少共增加多少边权. 题解:树形$DP$,对于每个点,如果它到它的子树中的叶子节点距离不同,一定要在这 ...
- 安徽师大附中%你赛day4T2 演讲解题报告
演讲 题目背景: 众所周知,\(\mathrm{Zdrcl}\)是一名天天\(\mathrm{AK}\)的高水平选手. 作为一民长者,为了向大家讲述自己\(\mathrm{AK}\)的经验,他决定在一 ...
- [模拟赛] StopAllSounds
Description 小松鼠开心地在树之间跳跃着,突然她停了下来.因为眼前出现了一个 拿着专克超萌小松鼠的法宝----超萌游戏机的游客! 超萌游戏机之所以拥有这个名字,是因为它的屏幕是一个n × 2 ...
- [codeforces gym Matrix God]随机矩阵乘法
题目链接:http://codeforces.com/gym/101341/problem/I 随机真是一个神奇的方法.原本矩阵乘法是n^3的复杂度,但是这个题是让判断两个矩阵是否相等,只需要在两个矩 ...
- LA2995 Image is everything
蓝书P12 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm ...
- mysql删除id最小的条目
DELETE FROM 表1 WHERE Mid in (select Mid from (SELECT Min(Mid) Mid FROM 表1 c1) t1);