Submission #896588


Source Code Expand

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

class Program
{
    const int M = 1000000007;
    const double eps = 1e-9;
    static int[] dd = { 0, 1, 0, -1, 0 };
    static void Main()
    {
        var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        var sc = new Scan();
        int r, c;
        sc.Multi(out r, out c);
        int n = sc.Int;
        var nodes = new SortedDictionary<int, int>[r];
        for (int i = 0; i < r; i++)
        {
            nodes[i] = new SortedDictionary<int, int>();
        }
        var min = new int[c];
        for (int i = 0; i < c; i++)
        {
            min[i] = M;
        }
        for (int i = 0; i < n; i++)
        {
            var a = sc.IntArr;
            --a[0];
            --a[1];
            nodes[a[0]].Add(a[1], a[2]);
            min[a[1]] = Math.Min(min[a[1]], a[2]);
        }
        var ed_to = new List<int>[c];
        var ed_cost = new List<int>[c];
        for (int i = 0; i < c; i++)
        {
            ed_to[i] = new List<int>();
            ed_cost[i] = new List<int>();
        }
        for (int i = 0; i < r; i++)
        {
            int prc = -1, prcost = -1;
            foreach (var item in nodes[i])
            {
                if (prc > -1)
                {
                    ed_to[prc].Add(item.Key);
                    ed_to[item.Key].Add(prc);
                    ed_cost[prc].Add(item.Value - prcost);
                    ed_cost[item.Key].Add(prcost - item.Value);
                }
                prc = item.Key;
                prcost = item.Value;
            }
        }
        var dis = new int[c];
        for (int i = 0; i < c; i++)
        {
            dis[i] = -M * 2;
        }
        for (int i = 0; i < c; i++)
        {
            if (dis[i] == -M * 2)
            {
                var q = new Queue<int>();
                q.Enqueue(i);
                dis[i] = 0;
                while (q.Any())
                {
                    var p = q.Dequeue();
                    for (int j = 0; j < ed_to[p].Count; j++)
                    {
                        var nex = ed_to[p][j];
                        if (dis[nex] == -M * 2)
                        {
                            dis[nex] = dis[p] + ed_cost[p][j];
                            q.Enqueue(nex);
                        }
                        else if (dis[nex] != dis[p] + ed_cost[p][j])
                        {
                            DBG("No");
                            return;
                        }
                        if (min[i] + dis[nex] < 0 || min[p] + ed_cost[p][j] < 0)
                        {
                            DBG("No");
                            return;
                        }
                        min[nex] = Math.Min(min[p] + ed_cost[p][j], min[nex]);
                    }
                }
            }
        }
        sw.WriteLine("Yes");
        sw.Flush();
    }

    static void swap<T>(ref T a, ref T b) { var t = a; a = b; b = t; }
    static void swap<T>(IList<T> a, int i, int j) { var t = a[i]; a[i] = a[j]; a[j] = t; }
    static T Max<T>(params T[] a) { return a.Max(); }
    static T Min<T>(params T[] a) { return a.Min(); }
    static void DBG<T>(params T[] a) { Console.WriteLine(string.Join(" ", a)); }
    static void DBG(params object[] a) { Console.WriteLine(string.Join(" ", a)); }
    static T[] copy<T>(IList<T> a)
    {
        var ret = new T[a.Count];
        for (int i = 0; i < a.Count; i++) ret[i] = a[i];
        return ret;
    }
}
class Scan
{
    public int Int { get { return int.Parse(Str); } }
    public long Long { get { return long.Parse(Str); } }
    public double Double { get { return double.Parse(Str); } }
    public string Str { get { return Console.ReadLine().Trim(); } }
    public int[] IntArr { get { return StrArr.Select(int.Parse).ToArray(); } }
    public int[] IntArrWithSep(char sep) { return Str.Split(sep).Select(int.Parse).ToArray(); }
    public long[] LongArr { get { return StrArr.Select(long.Parse).ToArray(); } }
    public double[] DoubleArr { get { return StrArr.Select(double.Parse).ToArray(); } }
    public string[] StrArr { get { return Str.Split(); } }
    T cv<T>(string inp)
    {
        if (typeof(T).Equals(typeof(int)))    return (T)Convert.ChangeType(int.Parse(inp), typeof(T));
        if (typeof(T).Equals(typeof(long)))   return (T)Convert.ChangeType(long.Parse(inp), typeof(T));
        if (typeof(T).Equals(typeof(double))) return (T)Convert.ChangeType(double.Parse(inp), typeof(T));
        if (typeof(T).Equals(typeof(char)))   return (T)Convert.ChangeType(inp[0], typeof(T));
        return (T)Convert.ChangeType(inp, typeof(T));
    }
    public void Multi<T>(out T a) { a = cv<T>(Str); }
    public void Multi<T, U>(out T a, out U b)
    { var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); }
    public void Multi<T, U, V>(out T a, out U b, out V c)
    { var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); }
    public void Multi<T, U, V, W>(out T a, out U b, out V c, out W d)
    { var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); }
    public void Multi<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e)
    { var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]); }
}
class mymath
{
    static int Mod = 1000000007;
    public void setMod(int m) { Mod = m; }
    public bool isprime(long a)
    {
        if (a < 2) return false;
        for (long i = 2; i * i <= a; i++) if (a % i == 0) return false;
        return true;
    }
    public bool[] sieve(int n)
    {
        var isp = new bool[n + 1];
        for (int i = 2; i <= n; i++) isp[i] = true;
        for (int i = 2; i * i <= n; i++) if (isp[i]) for (int j = i * i; j <= n; j += i) isp[j] = false;
        return isp;
    }
    public List<int> getprimes(int n)
    {
        var prs = new List<int>();
        var isp = sieve(n);
        for (int i = 2; i <= n; i++) if (isp[i]) prs.Add(i);
        return prs;
    }
    public long[][] E(int n)
    {
        var ret = new long[n][];
        for (int i = 0; i < n; i++)
        {
            ret[i] = new long[n];
            ret[i][i] = 1;
        }
        return ret;
    }
    public long[][] powmat(long[][] A, long n)
    {
        if (n == 0) return E(A.Length);
        var t = powmat(A, n / 2);
        if ((n & 1) == 0) return mulmat(t, t);
        return mulmat(mulmat(t, t), A);
    }
    public long[] mulmat(long[][] A, long[] x)
    {
        int n = A.Length, m = x.Length;
        var ans = new long[n];
        for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) ans[i] = (ans[i] + x[j] * A[i][j]) % Mod;
        return ans;
    }
    public long[][] mulmat(long[][] A, long[][] B)
    {
        int n = A.Length, m = B[0].Length, l = B.Length;
        var ans = new long[n][];
        for (int i = 0; i < n; i++)
        {
            ans[i] = new long[m];
            for (int j = 0; j < m; j++) for (int k = 0; k < l; k++) ans[i][j] = (ans[i][j] + A[i][k] * B[k][j]) % Mod;
        }
        return ans;
    }
    public long[] addmat(long[] x, long[] y)
    {
        int n = x.Length;
        var ans = new long[n];
        for (int i = 0; i < n; i++) ans[i] = (x[i] + y[i]) % Mod;
        return ans;
    }
    public long[][] addmat(long[][] A, long[][] B)
    {
        int n = A.Length, m = A[0].Length;
        var ans = new long[n][];
        for (int i = 0; i < n; i++) ans[i] = addmat(A[i], B[i]);
        return ans;
    }
    public long powmod(long a, long b)
    {
        if (a >= Mod) return powmod(a % Mod, b);
        if (a == 0) return 0;
        if (b == 0) return 1;
        var t = powmod(a, b / 2);
        if ((b & 1) == 0) return t * t % Mod;
        return t * t % Mod * a % Mod;
    }
    public long inv(long a) { return powmod(a, Mod - 2); }
    public long gcd(long a, long b)
    {
        while (b > 0) { var t = a % b; a = b; b = t; }
        return a;
    }
    public long lcm(long a, long b) { return a * (b / gcd(a, b)); }
    public long Comb(int n, int r)
    {
        if (n < 0 || r < 0 || r > n) return 0;
        if (n - r < r) r = n - r;
        if (r == 0) return 1;
        if (r == 1) return n;
        var numerator = new int[r];
        var denominator = new int[r];
        for (int k = 0; k < r; k++)
        {
            numerator[k] = n - r + k + 1;
            denominator[k] = k + 1;
        }
        for (int p = 2; p <= r; p++)
        {
            int pivot = denominator[p - 1];
            if (pivot > 1)
            {
                int offset = (n - r) % p;
                for (int k = p - 1; k < r; k += p)
                {
                    numerator[k - offset] /= pivot;
                    denominator[k] /= pivot;
                }
            }
        }
        long result = 1;
        for (int k = 0; k < r; k++) if (numerator[k] > 1) result = result * numerator[k] % Mod;
        return result;
    }
}

Submission Info

Submission Time
Task D - Grid and Integers
User riantkb
Language C# (Mono 4.6.2.0)
Score 0
Code Size 9320 Byte
Status WA
Exec Time 465 ms
Memory 43008 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 800
Status
AC × 5
AC × 57
WA × 12
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 0_04.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 0_04.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt, 1_32.txt, 1_33.txt, 1_34.txt, 1_35.txt, 1_36.txt, 1_37.txt, 1_38.txt, 1_39.txt, 1_40.txt, 1_41.txt, 1_42.txt, 1_43.txt, 1_44.txt, 1_45.txt, 1_46.txt, 1_47.txt, 1_48.txt, 1_49.txt, 1_50.txt, 1_51.txt, 1_52.txt, 1_53.txt, 1_54.txt, 1_55.txt, 1_56.txt, 1_57.txt, 1_58.txt, 1_59.txt, 1_60.txt, 1_61.txt, 1_62.txt, 1_63.txt
Case Name Status Exec Time Memory
0_00.txt AC 40 ms 3928 KB
0_01.txt AC 35 ms 3544 KB
0_02.txt AC 35 ms 3416 KB
0_03.txt AC 34 ms 3416 KB
0_04.txt AC 35 ms 3544 KB
1_00.txt AC 148 ms 31664 KB
1_01.txt AC 148 ms 31792 KB
1_02.txt AC 147 ms 31668 KB
1_03.txt AC 137 ms 31668 KB
1_04.txt AC 148 ms 31796 KB
1_05.txt AC 137 ms 31796 KB
1_06.txt AC 330 ms 35676 KB
1_07.txt AC 362 ms 35632 KB
1_08.txt AC 419 ms 43004 KB
1_09.txt AC 409 ms 43008 KB
1_10.txt AC 428 ms 40332 KB
1_11.txt AC 413 ms 40440 KB
1_12.txt AC 465 ms 40332 KB
1_13.txt AC 423 ms 40332 KB
1_14.txt AC 411 ms 40332 KB
1_15.txt WA 418 ms 40332 KB
1_16.txt AC 392 ms 40332 KB
1_17.txt AC 386 ms 40224 KB
1_18.txt AC 408 ms 40224 KB
1_19.txt AC 391 ms 39944 KB
1_20.txt WA 212 ms 20488 KB
1_21.txt AC 149 ms 20808 KB
1_22.txt AC 79 ms 16252 KB
1_23.txt AC 285 ms 30996 KB
1_24.txt WA 273 ms 22432 KB
1_25.txt AC 218 ms 21956 KB
1_26.txt AC 57 ms 9268 KB
1_27.txt AC 118 ms 20744 KB
1_28.txt WA 358 ms 30236 KB
1_29.txt AC 167 ms 22508 KB
1_30.txt WA 367 ms 32652 KB
1_31.txt AC 218 ms 29900 KB
1_32.txt AC 251 ms 19356 KB
1_33.txt WA 372 ms 32528 KB
1_34.txt AC 103 ms 18000 KB
1_35.txt AC 259 ms 20432 KB
1_36.txt AC 114 ms 19872 KB
1_37.txt WA 151 ms 14832 KB
1_38.txt AC 307 ms 32228 KB
1_39.txt AC 162 ms 24860 KB
1_40.txt AC 293 ms 30856 KB
1_41.txt AC 219 ms 31540 KB
1_42.txt AC 254 ms 20252 KB
1_43.txt AC 281 ms 29736 KB
1_44.txt WA 309 ms 26668 KB
1_45.txt AC 121 ms 17372 KB
1_46.txt WA 356 ms 31676 KB
1_47.txt AC 162 ms 20752 KB
1_48.txt WA 128 ms 15988 KB
1_49.txt AC 98 ms 17192 KB
1_50.txt WA 203 ms 22632 KB
1_51.txt AC 320 ms 25444 KB
1_52.txt AC 135 ms 25604 KB
1_53.txt AC 145 ms 24848 KB
1_54.txt AC 153 ms 21236 KB
1_55.txt AC 148 ms 15880 KB
1_56.txt AC 251 ms 26516 KB
1_57.txt AC 321 ms 26648 KB
1_58.txt WA 300 ms 28240 KB
1_59.txt AC 102 ms 20740 KB
1_60.txt AC 35 ms 3544 KB
1_61.txt AC 34 ms 3416 KB
1_62.txt AC 34 ms 3416 KB
1_63.txt AC 35 ms 3416 KB