hdoj--3666--THE MATRIX PROBLEM(差分约束+SPFA深搜)
THE MATRIX PROBLEM
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.
3 3 1 6 2 3 4 8 2 6 5 2 9
YES
- 超时代码:(应该是oj编译器问题或者就是AC的标准提高了)
- #include<cstdio>
- #include<iostream>
- #include<cstring>
- #include<queue>
- #include<cmath>
- #include<algorithm>
- using namespace std;
- #define MAXN 1000
- #define MAXM 500000+10
- #define INF 0x3f3f3f
- int dis[MAXN],vis[MAXN],used[MAXN],m,n;
- int head[MAXN],cnt;
- double map[MAXN][MAXN];
- double L,U;
- struct node
- {
- int u,v;
- double val;
- int next;
- }edge[MAXM];
- void init()
- {
- memset(head,-1,sizeof(head));
- memset(map,0,sizeof(map));
- cnt=0;
- }
- void add(int u,int v,int val)
- {
- node E={u,v,val,head[u]};
- edge[cnt]=E;
- head[u]=cnt++;
- }
- void getmap()
- {
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=m;j++)
- {
- scanf("%lf",&map[i][j]);
- add(j+n,i,log(U/map[i][j]));
- add(i,j+n,-log(L/map[i][j]));
- }
- }
- for(int i=1;i<=n+m;i++)
- add(0,i,0);
- }
- void SPFA()
- {
- memset(vis,0,sizeof(vis));
- memset(used,0,sizeof(used));
- memset(dis,INF,sizeof(dis));
- queue<int>q;
- q.push(0);
- dis[0]=0;
- used[0]++;
- vis[0]=1;
- while(!q.empty())
- {
- int u=q.front();
- q.pop();
- vis[u]=0;
- for(int i=head[u];i!=-1;i=edge[i].next)
- {
- node E=edge[i];
- if(dis[E.v]>dis[E.u]+E.val)
- {
- dis[E.v]=dis[E.u]+E.val;
- if(!vis[E.v])
- {
- vis[E.v]=1;
- used[E.v]++;
- if(used[E.v]>(int)sqrt(1.0*n+m))
- {
- cout<<"NO"<<endl;
- return ;
- }
- q.push(E.v);
- }
- }
- }
- }
- cout<<"YES"<<endl;
- }
- int main()
- {
- while(scanf("%d%d%lf%lf",&n,&m,&L,&U)!=EOF)
- {
- init();
- getmap();
- SPFA();
- }
- return 0;
- }
SPFA深搜版
- #include<iostream>
- #include<cstdio>
- #include<string.h>
- #include<algorithm>
- #include<math.h>
- #include<stack>
- #include<queue>
- using namespace std;
- const int MAX=805;
- struct node
- {
- int v,next;
- double c;
- }g[MAX*MAX];
- int adj[MAX];
- int n,m,e;
- double dis[MAX],l,u;
- bool vis[MAX],inStack[MAX];
- inline void add(int u,int v,double c)
- {
- g[e].v=v; g[e].c=c; g[e].next=adj[u]; adj[u]=e++;
- }
- bool spfa(int u)
- {
- int i,v;
- if(inStack[u])
- return false;
- inStack[u]=true;
- vis[u]=true;
- for(i=adj[u];i!=-1;i=g[i].next)
- {
- v=g[i].v;
- if(dis[v]>dis[u]+g[i].c)
- {
- dis[v]=dis[u]+g[i].c;
- if(!spfa(v))
- {
- return false;
- }
- }
- }
- inStack[u]=false;
- return true;
- }
- bool ok()
- {
- int i,u,v,cnt=0;
- memset(vis,0,sizeof(vis));
- memset(inStack,0,sizeof(inStack));
- for(i=0;i<=n+m;i++)
- {
- dis[i]=0;
- }
- for(i=1;i<=n+m;i++)
- {
- if(!vis[i])
- {
- if(!spfa(i))
- {
- return false;
- }
- }
- }
- return true;
- }
- int main()
- {
- int i,j;
- double t;
- while(scanf("%d%d %lf %lf",&n,&m,&l,&u)!=EOF)
- {
- e=0;
- memset(adj,-1,sizeof(adj));
- for(i=1;i<=n;i++)
- {
- for(j=1;j<=m;j++)
- {
- scanf("%lf",&t);
- add(j+n,i,log(u/t));
- add(i,j+n,-log(l/t));
- }
- }
- if(ok())
- puts("YES");
- else
- puts("NO");
- }
- return 0;
- }
hdoj--3666--THE MATRIX PROBLEM(差分约束+SPFA深搜)的更多相关文章
- HDU 3666 THE MATRIX PROBLEM (差分约束)
题意:给定一个最大400*400的矩阵,每次操作可以将某一行或某一列乘上一个数,问能否通过这样的操作使得矩阵内的每个数都在[L,R]的区间内. 析:再把题意说明白一点就是是否存在ai,bj,使得l&l ...
- HDU 3666.THE MATRIX PROBLEM 差分约束系统
THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 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 ...
- 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 ...
- hduTHE MATRIX PROBLEM(差分约束)
题目请戳这里 题目大意:给一个n*m的矩阵,求是否存在这样两个序列:a1,a2...an,b1,b2,...,bm,使得矩阵的第i行乘以ai,第j列除以bj后,矩阵的每一个数都在L和U之间. 题目分析 ...
- HDOJ 1016 Prime Ring Problem素数环【深搜】
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...
- HDU 3666 THE MATRIX PROBLEM (差分约束 深搜 & 广搜)
THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 【poj3169】【差分约束+spfa】
题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没 ...
- O - Layout(差分约束 + spfa)
O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...
随机推荐
- 2016.03.02,英语,《Vocabulary Builder》Unit 03
ambi/amphi: 指on both sides或者around的意思,ambi-来自拉丁语,amphi-来自希腊语.ambidextrous:[ˌæmbi'dekstrəs] adj. 两手俱利 ...
- jquery中命名冲突问题
例如用jq代替$符号 var jq = $.noConflict()
- C# 遍历文件夹及子目录下所有图片.
要求:取指定目录下面的所有图片,以表格的型式展示并显示该图片的相对路径. 服务端代码: public partial class ViewIcon : System.Web.UI.Page { JAr ...
- oracle数据泵备份与还原
完整的常用的一套oracle备份以及还原方案 --在新库中新建数据目录,我没有特别说明在哪执行的语句都可在plsql中执行 CREATE OR REPLACE DIRECTORY dump_dir A ...
- SQL Server的三种分页方式
直接上代码 --top not in方式 select top 条数 * from tablename where Id not in (select top 条数*页数 Id from tablen ...
- Python笔记(七)
# -*-coding:utf-8-*- # Python 文件I/O # 打印到屏幕 #print 1234567 # 读取屏幕输入 #input_str=raw_input("Pleas ...
- R dataframe 去除行号
原先的行号是这样的:
- golang vue nginx
https://segmentfault.com/a/1190000012780963 https://blog.csdn.net/qq_32340877/article/details/790321 ...
- .csv文件内容分隔符
CSV文件默认以英文逗号做为列分隔符,换行符作为行分隔符. 如果不提供网页形式只用命令行或二进制程序输出数据到CSV,只需要将数据段按,分割,行按\n分割,写入到一个.csv文件即可. 但有时字段 ...
- asp实现阿里大鱼短信API接口的方法
阿里大鱼是阿里推出的产品,官方提供JAVA..NET.PHP等版本的SDK下载,不知为何,唯独不提供ASP版本的SDK. 不提供没关系,自己写就是了,参照官方提供的API写一个就是了. 本来以为无非是 ...