110,533
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 最大流
{
class Program
{
static void Main(string[] args)
{
private const int maxN =201;
private static int[,] edge = new int[maxN, maxN];
private bool[] visited = new bool[maxN];
private int[] father = new int[maxN];
private int N; //边数,顶点数
private int M;
private int ans; //结果
private void Ford_Fulkerson()
{
while(1)
{
//一次大循环,找到一条可能的增广路径
Queue<int> q = new Queue<int>();
//C++ TO C# CONVERTER TODO TASK: The memory management function 'memset' has no equivalent in C#:
memset(visited, 0, sizeof(visited));
//C++ TO C# CONVERTER TODO TASK: The memory management function 'memset' has no equivalent in C#:
memset(father, -1, sizeof(father));
int now;
visited[0] = true;
q.Enqueue(0);
while(!q.Count == 0) //广度优先
{
now = q.Peek();
q.Dequeue();
if(now == M-1)
break;
for(int i = 0; i < M; i++)
{
//每次父亲节点都要更新,权值减为0的边就不算了.
if(edge[now, i] && !visited[i])
{
father[i] = now;
visited[i] = true;
q.Enqueue(i);
}
}
}
//可能的增广路不存在了
if(!visited[M-1])
break;
int u;
int min = 0xFFFF;
for(u = M-1; u; u = father[u]) //找出权值最小的边
{
if(edge[father[u], u] < min)
min = edge[father[u], u];
}
//减去最小权值
for(u = M-1; u; u = father[u])
{
//前向弧减去
edge[father[u], u] -= min;
//后向弧加上
//存在圆环,这句话关键
edge[u, father[u]] += min;
}
//当前增广路径增加的流
ans += min;
}
}
private static int Main()
{
int s;
int e;
int w;
while((N = SimulateCin.ReadToWhiteSpace(true)).Length > 0 && (M = SimulateCin.ReadToWhiteSpace(true)).Length > 0)
{
ans = 0;
//C++ TO C# CONVERTER TODO TASK: The memory management function 'memset' has no equivalent in C#:
memset(edge, 0, sizeof(edge));
for(int i = 0; i<N; i++)
{
s = int.Parse(ReadToWhiteSpace(true));
e = SimulateCin.ReadToWhiteSpace(true);
w = SimulateCin.ReadToWhiteSpace(true);
edge[s-1, e-1] += w;
}
Ford_Fulkerson();
Console.Write(ans);
Console.Write("\n");
}
return 0;
}
internal static class SimulateCin
{
private static bool goodlastread = false;
internal static bool LastReadWasGood
{
get
{
return goodlastread;
}
}
internal static string ReadToWhiteSpace(bool skipleadingwhitespace)
{
string input = "";
char nextchar;
if (skipleadingwhitespace)
{
while (char.IsWhiteSpace(nextchar = (char)Console.Read()))
{
}
input += nextchar;
}
while ( ! char.IsWhiteSpace(nextchar = (char)Console.Read()))
{
input += nextchar;
}
goodlastread = input.Length > 0;
return input;
}
}
}
}
}
void memset(int[] dest, int c, int count)
{
for (int i = 0; i < dest.Length; i++)
{
dest[i] = c;
}
}
void memset(bool[] dest, bool b, int count)
{
for (int i = 0; i < dest.Length; i++)
{
dest[i] = b;
}
}
这是我能想到的方法,或者你会有更好的方法,不过到时,请告诉我。
Buffer.SetByte()