c#写出的spfa

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

#region Using directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
#endregion

namespace 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();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值