【思维题 经典模型】cf632F. Magic Matrix

 非常妙的经典模型转化啊……

You're given a matrix A of size n × n.

Let's call the matrix with nonnegative elements magic if it is symmetric (so aij = aji), aii = 0 and aij ≤ max(aik, ajk) for all triples i, j, k. Note that i, j, k do not need to be distinct.

Determine if the matrix is magic.

As the input/output can reach very huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.outin Java.

Input

The first line contains integer n (1 ≤ n ≤ 2500) — the size of the matrix A.

Each of the next n lines contains n integers aij (0 ≤ aij < 109) — the elements of the matrix A.

Note that the given matrix not necessarily is symmetric and can be arbitrary.

Output

Print ''MAGIC" (without quotes) if the given matrix A is magic. Otherwise print ''NOT MAGIC".


题目大意

给出一个 N×N 的矩阵 G,要求判断其是否满足:

  1. G[i][i]=0;
  2. G[i][j]=G[j][i];
  3. 对于所有的 i,j,k,有 G[i][j]≤max(G[i][k],G[j][k])。

题目分析

这个矩阵的形式非常特殊,然而考场上只是一直在想$n^{2.66}$的鬼畜矩乘……

本题是经典模型的巧妙转化。这里给出的矩阵实际上是一个邻接矩阵的形式,而第三个限制条件说的就是:原图最小瓶颈生成树的距离矩阵等于原矩阵。

 1 #include<bits/stdc++.h>
 2 const int maxn = 2535;
 3 const int maxm = 7035;
 4 
 5 struct Edge
 6 {
 7     int v,val;
 8     Edge(int a=0, int b=0):v(a),val(b) {}
 9 }edges[maxm];
10 int n,a[maxn][maxn],f[maxn][maxn];
11 int edgeTot,head[maxn],nxt[maxm],pre[maxn],dis[maxn];
12 bool chk,vis[maxn];
13 
14 int read()
15 {
16     char ch = getchar();
17     int num = 0, fl = 1;
18     for (; !isdigit(ch); ch=getchar())
19         if (ch=='-') fl = -1;
20     for (; isdigit(ch); ch=getchar())
21         num = (num<<1)+(num<<3)+ch-48;
22     return num*fl;
23 }
24 void addedge(int u, int v, int c)
25 {
26     edges[++edgeTot] = Edge(v, c), nxt[edgeTot] = head[u], head[u] = edgeTot;
27     edges[++edgeTot] = Edge(u, c), nxt[edgeTot] = head[v], head[v] = edgeTot;
28 }
29 void dfs(int Top, int x, int fa, int c)
30 {
31     f[Top][x] = c;
32     for (int i=head[x]; i!=-1; i=nxt[i])
33     {
34         int v = edges[i].v;
35         if (v==fa) continue;
36         dfs(Top, v, x, std::max(c, edges[i].val));
37     }
38 }
39 int main()
40 {
41     memset(head, -1, sizeof head);
42     n = read();
43     for (int i=1; i<=n; i++)
44         for (int j=1; j<=n; j++)
45             a[i][j] = read();
46     chk = 1;
47     for (int i=1; i<=n&&chk; i++)
48         if (a[i][i]) chk = false;
49     for (int i=1; i<=n&&chk; i++)
50         for (int j=i+1; j<=n&&chk; j++)
51             if (a[i][j]!=a[j][i]) chk = false;
52     if (!chk) puts("NOT MAGIC");
53     else{
54         memset(dis, 0x3f3f3f3f, sizeof dis);
55         dis[1] = 0;
56         for (int T=1; T<=n; T++)
57         {
58             int pos = 0;
59             for (int i=1; i<=n; i++)
60                 if (dis[i] < dis[pos]&&!vis[i]) pos = i;
61             if (!pos) break;
62             vis[pos] = true;
63             if (pre[pos]) addedge(pos, pre[pos], dis[pos]);
64             for (int i=1; i<=n; i++)
65                 if (!vis[i]&&dis[i] > a[pos][i])
66                     dis[i] = a[pos][i], pre[i] = pos;
67         }
68         for (int i=1; i<=n; i++) dfs(i, i, i, 0);
69         for (int i=1; i<=n&&chk; i++)
70             for (int j=1; j<=n&&chk; j++)
71                 if (a[i][j]!=f[i][j]) chk = false;
72         puts(chk?"MAGIC":"NOT MAGIC");
73     }
74     return 0;
75 }

 

 

END

posted @ 2019-03-14 21:29  AntiQuality  阅读(305)  评论(0编辑  收藏  举报