关闭

Formyy

14人阅读 评论(0) 收藏 举报
分类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace yyv1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    int n, m, S, T;
    double ans;
    double[,] f = new double[20, 20];
    double[,] c = new double[20, 20];
    int[] fa = new int[20];
    double[] dp = new double[20];
    int[] h = new int[4000];
    bool[] vis = new bool[20];

    public void icnct(int x,int y,double flow,double cost)          //建图连边
    {
        f[x,y]=flow;
        c[x,y]=cost;
        f[y,x]=0;
        c[y,x]=-cost;
    }

    public bool ifound()                                            //寻找增广路
    {
        /*************************数组初始化*************************/
        for (int i=S;i<=T;i++){
            fa[i]=100000000;
            vis[i]=false;
            dp[i]=100000000;
        }
        /************************************************************/

        /********************寻求当前流的最小费用********************/
        int lef=0,rig=0,x;
        fa[S]=0;
        dp[S]=0;
        h[0]=S;
        vis[S]=true;
        while (lef<=rig){
            x=h[lef];
            for (int i=S;i<=T;i++)
                if (f[x,i]>0 && dp[i]>dp[x]+c[x,i]){
                    fa[i]=x;
                    dp[i]=dp[x]+c[x,i];
                    if (!vis[i])
                    {
                        vis[i] = true;
                        h[++rig] = i;
                    }
                }
            vis[x]=false;
            lef++;
        }
        /************************************************************/
        if (fa[T] < 100000000) return true;                         //说明当前可以流
        return false;
    }

    public void iadd(){                                             //当前最小费用流
        int x=T,y;
        double minfl=100000000;
        /************寻求当前最小费用下一次可流的最大流量************/
        while (x!=S){
            y=fa[x];
            if (f[y,x]<minfl) minfl=f[y,x];
            x=y;
        }
        /************************************************************/

        /*************依次流当前最小费用下可流的最大流量*************/
        x=T;
        while (x!=S){
            y=fa[x];
            f[y,x]-=minfl;
            f[x,y]+=minfl;
            x=y;
        }
        /************************************************************/

        ans+=dp[T]*minfl;                                           //更新答案
    }


    private void bg_Click(object sender, EventArgs e)
    {
        n = 6;
        m = 6;
        S = 0;                              // S:源点
        T = n + m + 1;                      // T: 汇点

        /*************************数组初始化*************************/
        for (int i = S; i <= T; i++)
            for (int j = S; j <= T; j++)
            {
                f[i, j] = 0;
                c[i, j] = 100000000;
            }
        /************************************************************/

        /************************各点有土方量************************/
        double tot=0;
        double w;
        if (tbw1.Text == "") w = 0;
        else w = double.Parse(tbw1.Text);
        icnct(S, 1, w, 0);
        tot += w;
        if (tbw2.Text == "") w = 0;
        else w = double.Parse(tbw2.Text);
        icnct(S, 2, w, 0);
        tot += w;
        if (tbw3.Text == "") w = 0;
        else w = double.Parse(tbw3.Text);
        icnct(S, 3, w, 0);
        tot += w;
        if (tbw4.Text == "") w = 0;
        else w = double.Parse(tbw4.Text);
        icnct(S, 4, w, 0);
        tot += w;
        if (tbw5.Text == "") w = 0;
        else w = double.Parse(tbw5.Text);
        icnct(S, 5, w, 0);
        tot += w;
        if (tbw6.Text == "") w = 0;
        else w = double.Parse(tbw6.Text);
        icnct(S, 6, w, 0);
        tot += w;
        /************************************************************/

        /************************各点需土方量************************/
        double tot2 = 0;
        if (tbt1.Text == "") w = 0;
        else w = double.Parse(tbt1.Text);
        icnct(7, T, w, 0);
        tot2 += w;
        if (tbt2.Text == "") w = 0;
        else w = double.Parse(tbt2.Text);
        icnct(8, T, w, 0);
        tot2 += w;
        if (tbt3.Text == "") w = 0;
        else w = double.Parse(tbt3.Text);
        icnct(9, T, w, 0);
        tot2 += w;
        if (tbt4.Text == "") w = 0;
        else w = double.Parse(tbt4.Text);
        icnct(10, T, w, 0);
        tot2 += w;
        if (tbt5.Text == "") w = 0;
        else w = double.Parse(tbt5.Text);
        icnct(11, T, w, 0);
        tot2 += w;
        if (tbt6.Text == "") w = 0;
        else w = double.Parse(tbt6.Text);
        icnct(12, T, w, 0);
        tot2 += w;
        /************************************************************/

        string rst = "";
        if (tot2 > tot) rst="填方土量大于挖方土量,需要另外调土!";

        /***********************运土方单位价格***********************/
        if (tb11.Text == "") w = 100000000;
        else w = double.Parse(tb11.Text);
        icnct(1, 7, 100000000, w);
        if (tb12.Text == "") w = 100000000;
        else w = double.Parse(tb12.Text);
        icnct(1, 8, 100000000, w);
        if (tb13.Text == "") w = 100000000;
        else w = double.Parse(tb13.Text);
        icnct(1, 9, 100000000, w);
        if (tb14.Text == "") w = 100000000;
        else w = double.Parse(tb14.Text);
        icnct(1, 10, 100000000, w);
        if (tb15.Text == "") w = 100000000;
        else w = double.Parse(tb15.Text);
        icnct(1, 11, 100000000, w);
        if (tb16.Text == "") w = 100000000;
        else w = double.Parse(tb16.Text);
        icnct(1, 12, 100000000, w);
        if (tb21.Text == "") w = 100000000;
        else w = double.Parse(tb21.Text);
        icnct(2, 7, 100000000, w);
        if (tb22.Text == "") w = 100000000;
        else w = double.Parse(tb22.Text);
        icnct(2, 8, 100000000, w);
        if (tb23.Text == "") w = 100000000;
        else w = double.Parse(tb23.Text);
        icnct(2, 9, 100000000, w);
        if (tb24.Text == "") w = 100000000;
        else w = double.Parse(tb24.Text);
        icnct(2, 10, 100000000, w);
        if (tb25.Text == "") w = 100000000;
        else w = double.Parse(tb25.Text);
        icnct(2, 11, 100000000, w);
        if (tb26.Text == "") w = 100000000;
        else w = double.Parse(tb26.Text);
        icnct(2, 12, 100000000, w);
        if (tb31.Text == "") w = 100000000;
        else w = double.Parse(tb31.Text);
        icnct(3, 7, 100000000, w);
        if (tb32.Text == "") w = 100000000;
        else w = double.Parse(tb32.Text);
        icnct(3, 8, 100000000, w);
        if (tb33.Text == "") w = 100000000;
        else w = double.Parse(tb33.Text);
        icnct(3, 9, 100000000, w);
        if (tb34.Text == "") w = 100000000;
        else w = double.Parse(tb34.Text);
        icnct(3, 10, 100000000, w);
        if (tb35.Text == "") w = 100000000;
        else w = double.Parse(tb35.Text);
        icnct(3, 11, 100000000, w);
        if (tb36.Text == "") w = 100000000;
        else w = double.Parse(tb36.Text);
        icnct(3, 12, 100000000, w);
        if (tb41.Text == "") w = 100000000;
        else w = double.Parse(tb41.Text);
        icnct(4, 7, 100000000, w);
        if (tb42.Text == "") w = 100000000;
        else w = double.Parse(tb42.Text);
        icnct(4, 8, 100000000, w);
        if (tb43.Text == "") w = 100000000;
        else w = double.Parse(tb43.Text);
        icnct(4, 9, 100000000, w);
        if (tb44.Text == "") w = 100000000;
        else w = double.Parse(tb44.Text);
        icnct(4, 10, 100000000, w);
        if (tb45.Text == "") w = 100000000;
        else w = double.Parse(tb45.Text);
        icnct(4, 11, 100000000, w);
        if (tb46.Text == "") w = 100000000;
        else w = double.Parse(tb46.Text);
        icnct(4, 12, 100000000, w);
        if (tb51.Text == "") w = 100000000;
        else w = double.Parse(tb51.Text);
        icnct(5, 7, 100000000, w);
        if (tb52.Text == "") w = 100000000;
        else w = double.Parse(tb52.Text);
        icnct(5, 8, 100000000, w);
        if (tb53.Text == "") w = 100000000;
        else w = double.Parse(tb53.Text);
        icnct(5, 9, 100000000, w);
        if (tb54.Text == "") w = 100000000;
        else w = double.Parse(tb54.Text);
        icnct(5, 10, 100000000, w);
        if (tb55.Text == "") w = 100000000;
        else w = double.Parse(tb55.Text);
        icnct(5, 11, 100000000, w);
        if (tb56.Text == "") w = 100000000;
        else w = double.Parse(tb56.Text);
        icnct(5, 12, 100000000, w);
        if (tb61.Text == "") w = 100000000;
        else w = double.Parse(tb61.Text);
        icnct(6, 7, 100000000, w);
        if (tb62.Text == "") w = 100000000;
        else w = double.Parse(tb62.Text);
        icnct(6, 8, 100000000, w);
        if (tb63.Text == "") w = 100000000;
        else w = double.Parse(tb63.Text);
        icnct(6, 9, 100000000, w);
        if (tb64.Text == "") w = 100000000;
        else w = double.Parse(tb64.Text);
        icnct(6, 10, 100000000, w);
        if (tb65.Text == "") w = 100000000;
        else w = double.Parse(tb65.Text);
        icnct(6, 11, 100000000, w);
        if (tb66.Text == "") w = 100000000;
        else w = double.Parse(tb66.Text);
        icnct(6, 12, 100000000, w);
        /************************************************************/

        ans = 0;
        while (ifound()) iadd();

        /**************************输出结果**************************/
        rst = "最小的总价:"+ans.ToString()+"\n"+"最优的调配方案如下, 第i行第j列的元素表示点i向点j运土方量:\n\t";
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                rst = rst + f[j + m, i].ToString() + "\t";
            }
            rst = rst + "\n\t";
        }
        MessageBox.Show(rst);
        /************************************************************/
    }

    private void btn_cancel_Click(object sender, EventArgs e)       //关闭窗口
    {
        this.Close();
    }

    private void btn_help_Click(object sender, EventArgs e)
    {
        string helpshow = "\t\t土方调运程序填写须知:\n\tw1-w6填写6地挖方土量(不填默认无土量)。\n\tt1-t6填写6地填方土量(不填默认不需土量)。\n";
        helpshow += "\t中间6×6矩阵填写土方调运单价(不填默认不可运)。";
        MessageBox.Show(helpshow);
    }

    private void label14_Click(object sender, EventArgs e)
    {

    }


}

}

0
0

  相关文章推荐
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:14次
    • 积分:10
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档
    阅读排行
    评论排行