HDU-3666 THE MATRIX PROBLEM (差分约束)

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3666

代码:

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<algorithm>
  4 #include<vector>
  5 #include<map>
  6 #include<queue>
  7 #include<set>
  8 #include<cmath>
  9 #include<list>
 10 #include<cstring>
 11 #include<string>
 12 #include<stack>
 13 #define ll long long
 14 #define ull unsigned long long
 15 #define inf 0x3f3f3f3f
 16 #define inff 0x7fffffff
 17 using namespace std;
 18 const int N = 800 + 10;
 19 
 20 int n, m;
 21 
 22 struct node {
 23     int to, next;
 24     double w;
 25 }edge[N * (N + 1)];
 26 
 27 int head[N];
 28 int cnt;
 29 
 30 void add(int u, int v, double w) {
 31     edge[cnt].w = w;
 32     edge[cnt].to = v;
 33     edge[cnt].next = head[u];
 34     head[u] = cnt++;
 35 }
 36 
 37 int In[N];
 38 double dis[N];
 39 int vis[N];
 40 
 41 bool spfa(int s) {
 42 
 43     queue<int>q;
 44     memset(vis, 0, sizeof(vis));
 45     memset(dis, 0x3f, sizeof(dis));
 46     memset(In, 0, sizeof(In));
 47 
 48     q.push(s);
 49     vis[s] = 1;
 50     dis[s] = 0;
 51     In[s]++;
 52 
 53     while (!q.empty()) {
 54 
 55         int tmp = q.front();
 56         q.pop();
 57         vis[tmp] = 0;
 58         for (int i = head[tmp]; i != -1; i = edge[i].next) {
 59 
 60             int to = edge[i].to;
 61             double disto = edge[i].w;
 62             if (dis[to] > dis[tmp] + disto) {
 63                 dis[to] = dis[tmp] + disto;
 64                 if (!vis[to]) {
 65                     vis[to] = 1;
 66                     q.push(to);
 67                     if (++In[to] > sqrt(n + m)) return false;
 68                 }
 69             }
 70 
 71         }
 72     }
 73 
 74     return true;
 75 }
 76 
 77 int main() {
 78 
 79     /*ios::sync_with_stdio(false);
 80     cin.tie(0), cout.tie(0);*/
 81 
 82     double L, U;
 83     while (~scanf("%d%d%lf%lf", &n, &m, &L, &U)) {
 84         cnt = 0;
 85         memset(head, -1, sizeof(head));
 86         memset(edge, 0, sizeof(edge));
 87         double w1, w2, x;
 88         double logu = log(U);
 89         double logl = log(L);
 90         for (int i = 1; i <= n; i++) {
 91             for (int j = 1; j <= m; j++) {
 92                 scanf("%lf", &x);
 93                 w1 = logu - log(x);
 94                 w2 = log(x) - logl;
 95                 //cout << w1 << " " << w2 << "\n";
 96                 add(n + j, i, w1);
 97                 add(i, n + j, w2);
 98             }
 99         }
100         for (int i = 1; i <= n + m; i++) {
101             add(0, i, 0);
102         }
103         if (spfa(0)) printf("YES\n");
104         else printf("NO\n");
105     }
106 
107     return 0;
108 }

 

 
posted @ 2022-08-01 11:06  Keyzee  阅读(19)  评论(0编辑  收藏  举报