2014
11-30

# THE MATRIX PROBLEM

You have been given a matrix CN*M, each element E of CN*M 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.

There are several test cases. You should process to the end of file.
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.

There are several test cases. You should process to the end of file.
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


spfa判断存在负权环:

(1) 单个点入队列的次数大于 sqrt(N)  ， N代表所有点的个数

(2)  点入队列的次数总和大于 T*N  ，据说T一般取2.

《算法导论》里面说构造一个原点，他到其他每个点的边的长度为0,然后再SPFA，

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;

const double MAX=0x3f3f3f3f;
int n,m;
double l,r;
struct node
{
int v,nxt;
double w;
}edge[800000];
int head[1000],nume;

void add(int u,int v,double w)
{
edge[nume].v=v;edge[nume].w=w;edge[nume].nxt=head[u];head[u]=nume++;
}
int spfa(){
double dis[1000];
int inque[1000],i;
queue<int >q;
for(i=0;i<=n+m;i++){
dis[i]=0;
inque[i]=0;
q.push(i);
}
int sum=1;
while(!q.empty()){
int temp=q.front();
q.pop();
inque[temp]=0;
for(i=head[temp];i!=-1;i=edge[i].nxt){
int v=edge[i].v;
if(dis[temp]+edge[i].w<dis[v]){
dis[v]=dis[temp]+edge[i].w;
if(!inque[v]){
q.push(v);
inque[v]=1;
sum++;
if(sum>2*(n+m)) return 0;
}
}
}

}
return 1;

}

int main()
{

while(scanf("%d%d%lf%lf",&n,&m,&l,&r)!=EOF){
int i,j;
nume=0;
memset(head,-1,sizeof(head));
// for(i=1;i<=n+m;i++) add(0,i,0);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++){
double temp;
scanf("%lf",&temp);
add(n+j,i,log10(r)-log10(temp));
add(i,n+j,log10(temp)-log10(l));
}
if(spfa()) printf("YES\n");
else printf("NO\n");
}
return 0;
}


1. #!/usr/bin/env python
def cou(n):
arr =
i = 1
while(i<n):
arr.append(arr[i-1]+selfcount(i))
i+=1
return arr[n-1]

def selfcount(n):
count = 0
while(n):
if n%10 == 1:
count += 1
n /= 10
return count

2. 第2题，TCP不支持多播，多播和广播仅应用于UDP。所以B选项是不对的。第2题，TCP不支持多播，多播和广播仅应用于UDP。所以B选项是不对的。