static void Main(String[] args)
    {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
        int n = Convert.ToInt32(Console.ReadLine());

        for (int i = 0; i < n; i++)
        {
            string   str = Console.ReadLine();
            string[] num = str.Split(' ');
            Graph    g   = new Graph(Convert.ToInt32(num[0]));
            int      e   = Convert.ToInt32(num[1]);
            for (int j = 0; j < e; j++)
            {
                str = Console.ReadLine();
                num = str.Split(' ');
                g.addEdge(Convert.ToInt32(num[0]) - 1, Convert.ToInt32(num[1]) - 1);
            }
            int s = Convert.ToInt32(Console.ReadLine()) - 1;
            //Console.WriteLine("F**k {0}", s);
            BreadthFirstPaths bfs = new BreadthFirstPaths(g, s);

            for (int j = 0; j < g.V(); j++)
            {
                if (bfs.hasPath(j) && j != s)
                {
                    Console.Write("{0} ", bfs.DistTo(j) * 6);
                }
                else if (!bfs.hasPath(j) && j != s)
                {
                    Console.Write("{0} ", -1);
                }
            }
            Console.WriteLine();
        }
    }
예제 #2
0
        public void BreadthFirstPathsTest1()
        {
            Graph             g = TestHelpers.buildGraph();
            BreadthFirstPaths bfps;

            bfps = new BreadthFirstPaths(g, 0);

            Assert.AreEqual(0, bfps.DistTo(0));
            Assert.AreEqual(1, bfps.DistTo(1));
            Assert.AreEqual(3, bfps.DistTo(4));

            Assert.AreEqual(int.MaxValue, bfps.DistTo(10));
            Assert.AreEqual(int.MaxValue, bfps.DistTo(12));
            Assert.AreEqual(int.MaxValue, bfps.DistTo(5));

            bfps = new BreadthFirstPaths(g, 10);
            Assert.IsTrue(bfps.HasPathTo(11));
            Assert.AreEqual(1, bfps.DistTo(11));

            bfps = new BreadthFirstPaths(g, 12);
            Assert.AreEqual(2, bfps.DistTo(15));

            Assert.IsFalse(bfps.HasPathTo(11));
            Assert.IsFalse(bfps.HasPathTo(3));
            Assert.IsFalse(bfps.HasPathTo(9));
        }
예제 #3
0
    void Start()
    {
        Graph G = new Graph(txt);

        int s = 0;
        BreadthFirstPaths bfs = new BreadthFirstPaths(G, s);

        for (int v = 0; v < G.V(); v++)
        {
            if (bfs.hasPathTo(v))
            {
                //print("%d to %d (%d):  ", s, v, bfs.DistTo(v));
                string str = s + " to " + v + "\tdistance:" + bfs.DistTo(v) + "\t.\t";
                foreach (int x in bfs.pathTo(v))
                {
                    if (x == s)
                    {
                        str += x;
                    }
                    else
                    {
                        str += ("-" + x);
                    }
                }
                print(str);
            }

            else
            {
                print(s + " to " + v + ":  not connected\n");
            }
        }
    }