c#写出的spfa

来源:互联网 发布:unsw moodle 网络 编辑:程序博客网 时间:2024/06/05 02:00

C#自己(似乎)是不内置队列的,所以手动实现了一个简易的队列。
手写scanf,但是并不会实现清屏操作,所以输入时很鬼畜……
就是这样了。

#region Using directivesusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using static System.Console;#endregionnamespace ConsoleApplication1{    class Queue    {        protected static int[] num = new int[99999];        protected int f;        protected int b;        public void clear()        {            for(int i=0;i<=99998;i++)            {                num[i] = 0;            }            f = 0;            b = 0;        }        public void push(int n)        {            num[f] = n;            f++;            if (f == 99998) f = 0;        }         public int pop()        {            int k = b;            b++;            if (b == 99998) b = 0;            return num[k];        }        public bool empty()        {            return (f == b);        }    };    struct Edge    {        public int ff;        public int tt;        public int dd;        public int nxt;    };    class Program    {        public static int[] dist = new int[6300];        public static bool[] passby = new bool[6300];        public static Edge[] edge = new Edge[20000];        public static int T, C, Ts, Te;        public static int tot = 0;        public static int[] head = new int[6300];        static void print(int a)        {            WriteLine($"{dist[a]}");        }        static void add(int f, int t, int d)        {            edge[++tot].ff = f;            edge[tot].tt = t;            edge[tot].dd = d;            edge[tot].nxt = head[f];            head[f] = tot;        }        static void scanf(int a)        {            int i = 0;            a = 0;            char c = '0';            while(c != '\n' && c != ' ')            {                i++;                a *= 10;                a += (c - '0');                c = ReadKey().KeyChar;            }            while(i!=0)            {                i--;                WriteLine("6666666666666");            }        }        static void spfa()        {            Queue q = new Queue();            q.clear();            dist[Ts] = 0;            passby[Ts] = true;            q.push(Ts);            while(!q.empty())            {                int x = q.pop();                passby[x] = false;                for(int i=head[x];i!=0;i=edge[i].nxt)                {                    Edge e = edge[i];                    if(dist[e.tt]>dist[x]+e.dd)                    {                        dist[e.tt] = dist[x] + e.dd;                        if(!passby[e.tt])                        {                            q.push(e.tt);                            passby[e.tt] = true;                        }                    }                }            }        }        static void Main(string[] args)        {            T = 0;            C = 0;            Ts = 0;            Te = 0;            scanf(T);            scanf(C);            scanf(Ts);            scanf(Te);            for(int i=1;i<=C;i++)            {                int f = 0, t = 0, d = 0;                scanf(f);                scanf(t);                scanf(d);                add(f, t, d);                add(t, f, d);            }            for(int i=0;i<=T;i++)            {                dist[i] = 0x7fffffff;                passby[i] = false;            }            spfa();            print(Te);            ReadKey();        }    }}
0 0
原创粉丝点击