結果

問題 No.748 yuki国のお財布事情
ユーザー sekiya9311sekiya9311
提出日時 2018-10-20 14:29:32
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 288 ms / 2,000 ms
コード長 9,260 bytes
コンパイル時間 2,492 ms
コンパイル使用メモリ 112,000 KB
実行使用メモリ 25,216 KB
最終ジャッジ日時 2024-11-19 02:34:21
合計ジャッジ時間 6,193 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
19,328 KB
testcase_01 AC 36 ms
19,328 KB
testcase_02 AC 37 ms
19,200 KB
testcase_03 AC 35 ms
19,200 KB
testcase_04 AC 37 ms
19,328 KB
testcase_05 AC 37 ms
19,200 KB
testcase_06 AC 37 ms
19,200 KB
testcase_07 AC 38 ms
19,456 KB
testcase_08 AC 37 ms
19,200 KB
testcase_09 AC 37 ms
19,328 KB
testcase_10 AC 37 ms
19,328 KB
testcase_11 AC 37 ms
19,072 KB
testcase_12 AC 38 ms
19,328 KB
testcase_13 AC 68 ms
23,200 KB
testcase_14 AC 97 ms
23,168 KB
testcase_15 AC 73 ms
23,392 KB
testcase_16 AC 134 ms
23,424 KB
testcase_17 AC 229 ms
24,320 KB
testcase_18 AC 267 ms
24,704 KB
testcase_19 AC 288 ms
25,216 KB
testcase_20 AC 260 ms
24,704 KB
testcase_21 AC 247 ms
24,704 KB
testcase_22 AC 33 ms
18,944 KB
testcase_23 AC 33 ms
19,200 KB
testcase_24 AC 34 ms
18,944 KB
testcase_25 AC 238 ms
24,320 KB
testcase_26 AC 242 ms
24,704 KB
testcase_27 AC 238 ms
24,832 KB
testcase_28 AC 213 ms
24,064 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProgrammingContest
{
    class Writer : IDisposable
    {
        private System.IO.TextWriter Out { get; }
        private StringBuilder Sb { get; }
        private bool IsReactive { get; }
        public Writer(string path)
            : this(new System.IO.StreamWriter(path))
        {
        }
        public Writer(bool isReactive)
            : this(null, isReactive)
        {
        }
        public Writer(System.IO.TextWriter writer = null, bool isReactive = false)
        {
            this.Out = (writer ?? Console.Out);
            this.IsReactive = isReactive;
            if (!this.IsReactive)
            {
                this.Sb = new StringBuilder();
            }
        }
        public void Dispose()
        {
            if (!this.IsReactive)
            {
                this.Out.Write(Sb.ToString());
            }
            if (!this.Out.Equals(Console.Out))
            {
                this.Out.Dispose();
            }
        }
        public void Write(object val)
        {
            if (this.IsReactive)
            {
                this.Out.Write(val.ToString());
                this.Out.Flush();
            }
            else
            {
                this.Sb.Append(val.ToString());
            }
        }
        public void WriteFormat(string format, params object[] vals)
        {
            if (this.IsReactive)
            {
                this.Out.Write(format, vals);
                this.Out.Flush();
            }
            else
            {
                this.Sb.AppendFormat(format, vals);
            }
        }
        public void WriteLine(object val = null)
        {
            this.WriteFormat((val ?? string.Empty).ToString() + Environment.NewLine);
        }
        public void WriteLine(int val)
        {
            this.WriteLine(val.ToString());
        }
        public void WriteLine(long val)
        {
            this.WriteLine(val.ToString());
        }
        public void WriteLine(string val)
        {
            this.WriteLine((object)val);
        }
        public void WriteLine(string format, params object[] vals)
        {
            this.WriteFormat(format + Environment.NewLine, vals);
        }
    }

    class Scanner : IDisposable
    {
        private Queue<string> Buffer { get; }
        private char[] Sep { get; }
        private System.IO.TextReader Reader { get; }
        public Scanner(string path, char[] sep = null)
            : this(new System.IO.StreamReader(path), sep)
        {
        }
        public Scanner(System.IO.TextReader reader = null,
                        char[] sep = null)
        {
            this.Buffer = new Queue<string>();
            this.Sep = (sep ?? new char[] { ' ' });
            this.Reader = (reader ?? Console.In);
        }
        private void CheckBuffer()
        {
            if (this.Buffer.Count == 0 && this.Reader.Peek() != -1)
            {
                string str = string.Empty;
                for (; string.IsNullOrEmpty(str) || string.IsNullOrWhiteSpace(str);
                    str = this.Reader.ReadLine()) ;

                var strs = str.Split(this.Sep).Where(el => !(string.IsNullOrEmpty(el) || string.IsNullOrWhiteSpace(el)));
                foreach (var el in strs)
                {
                    this.Buffer.Enqueue(el);
                }
            }
        }

        public void Dispose()
        {
            if (!this.Reader.Equals(Console.In))
            {
                this.Reader.Dispose();
            }
        }

        public string Next()
        {
            this.CheckBuffer();
            return this.Buffer.Dequeue();
        }
        public string[] GetStringArray(int N)
        {
            return Enumerable.Range(0, N)
                             .Select(e => this.Next())
                             .ToArray();
        }

        public int NextInt()
        {
            return int.Parse(this.Next());
        }
        public int[] GetIntArray(int N)
        {
            return Enumerable.Range(0, N)
                             .Select(e => this.NextInt())
                             .ToArray();
        }

        public double NextDouble()
        {
            return double.Parse(this.Next());
    }
        public double[] GetdoubleArray(int N)
        {
            return Enumerable.Range(0, N)
                             .Select(e => this.NextDouble())
                             .ToArray();
        }

        public long NextLong()
        {
            return long.Parse(this.Next());
        }
        public long[] GetLongArray(int N)
        {
            return Enumerable.Range(0, N)
                             .Select(e => this.NextLong())
                             .ToArray();
        }

        public bool IsEnd
        {
            get
            {
                this.CheckBuffer();
                return this.Buffer.Count == 0;
            }
        }
    }

    class MainClass : IDisposable
    {
        private Scanner Sc { get; }
        private Writer Wr { get; }
        static string BackPath { get; } = "..";
        private char DirSep { get; } = System.IO.Path.DirectorySeparatorChar;
        private string InFilePath { get; } = string.Empty;
        private string OutFilePath { get; } = string.Empty;
        public MainClass()
        {
            this.InFilePath = MainClass.BackPath
                            + this.DirSep
                            + MainClass.BackPath
                            + this.DirSep
                            + "in.txt";
            this.OutFilePath = MainClass.BackPath
                             + this.DirSep
                             + MainClass.BackPath
                             + this.DirSep
                             + "out.txt";

            this.Wr = new Writer(this.IsReactive);
            //this.Wr = new Writer(this.OutFilePath);
#if DEBUG
            if (!this.IsReactive)
            {
                this.Sc = new Scanner(this.InFilePath);
            }
            else
            {
                this.Sc = new Scanner();
            }
#else
            this.Sc = new Scanner();
#endif
        }
        static void Main(string[] args)
        {
            using (var mainClass = new MainClass())
            {
                mainClass.Solve();
            }
        }

        public void Dispose()
        {
            this.Sc?.Dispose();
            this.Wr?.Dispose();
#if DEBUG
            Console.WriteLine("press any key to continue...");
            Console.ReadKey();
#endif
        }

        struct Edge: IComparable<Edge>
        {
            public int From { get; set; }
            public int To { get; set; }
            public long Cost { get; set; }

            public int CompareTo(Edge other)
            {
                return this.Cost.CompareTo(other.Cost);
            }
        }

        void Solve()
        {
            int N = Sc.NextInt();
            int M = Sc.NextInt();
            int K = Sc.NextInt();
            Edge[] edges = new Edge[M];
            for (int i = 0; i < M; i++)
            {
                int a = Sc.NextInt();
                int b = Sc.NextInt();
                int c = Sc.NextInt();
                edges[i] = new Edge()
                {
                    From = a - 1,
                    To = b - 1,
                    Cost = c
                };
            }
            foreach (var e in Sc.GetIntArray(K).Select(i => i - 1))
            {
                edges[e].Cost = 0;
            }
            Array.Sort(edges);
            UnionFind uf = new UnionFind(N);
            long ans = 0;
            foreach (var e in edges)
            {
                if (!uf.Unite(e.From, e.To))
                {
                    ans += e.Cost;
                }
            }
            Wr.WriteLine(ans);
        }

        private bool IsReactive { get; } = false; // TODO: reactive check !!
    }

    class UnionFind
    {
        private int[] Uni { get; }
        int count_;
        public UnionFind(int n)
        {
            this.Uni = new int[n];
            for (int i = 0; i < n; i++)
            {
                this.Uni[i] = -1;
            }
            this.count_ = n;
        }
        public int Count()
        {
            return this.count_;
        }
        public int Count(int n)
        {
            return -this.Uni[this.Find(n)];
        }
        public int Find(int n)
        {
            return (this.Uni[n] < 0 ? n : this.Uni[n] = this.Find(this.Uni[n]));
        }
        public bool Unite(int a, int b)
        {
            a = this.Find(a);
            b = this.Find(b);
            if (a == b)
            {
                return false;
            }
            if (this.Uni[a] > this.Uni[b])
            {
                int t = a;
                a = b;
                b = t;
            }
            this.Uni[a] += this.Uni[b];
            this.Uni[b] = a;
            this.count_--;
            return true;
        }
        public bool Same(int a, int b)
        {
            return this.Find(a) == this.Find(b);
        }
    }
}
0