You are on page 1of 5

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lab07
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("BT2");
Ex2 ex2 = new Ex2("E:\\LTĐT\\Text\\NganNhat.INP");// gọi class EX2 truyền file vào ex2
ex2.Solve("E:\\LTĐT\\Text\\NganNhat.OUT"); // giải ex2 bằng hàm Solve rồi in kết quả vào file
Console.WriteLine("BT3");
Ex3 ex3 = new Ex3("E:\\LTĐT\\Text\\FloyWarshall.INP");// gọi class Ex3 truyền file vào ex3
ex3.Solve("E:\\LTĐT\\Text\\FloyWarshall.OUT");// giải ex3 bằng hàm Solve và in kết quả vào file
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lab07
{
internal class Ex2
{
private int INF = 9999; // khai báo biến INF vô hạn
private int v;
private int e;
private int s;
private int t;
private int a;
private LinkedList<Tuple<int, int, int>> edgeList;// khai báo một danh sách cạnh
private int[,] dist;
private int[,] pre;
public Ex2(string path)
{
if (!File.Exists(path)) // kiểm tra xem có file tồn tại hay không
{
throw new FileNotFoundException(path);
}
string[] lines = File.ReadAllLines(path);// khai báo mảng lines đọc dữ liệu trong file
string[] args = lines[0].Split(); // gán giá trị đầu tiên của mảng lines vào mảng args
v = int.Parse(args[0]); // gán giá trị đầu tiên của mảng args cho biến v (đỉnh)
e = int.Parse(args[1]);// gán giá trị thứ 1 của mảng args cho biến e (cạnh)
s = int.Parse(args[2]);// gán giá trị thứ 2 của mảng args cho biến s (đỉnh bắt đầu)
t = int.Parse(args[3]);// gán giá trị thứ 3 của mảng args cho biến t (đỉnh kết thúc)
a = int.Parse(args[4]);// gán giá trị thứ 4 của mảng args cho biến a (đỉnh trung gian)
edgeList = new LinkedList<Tuple<int, int, int>>(); //khai báo một danh sách cạnh
for (int i = 1; i <= e; i++) // vòng lặp chạy từ 1 đến số cạnh của đồ thị
{
string[] numbers = lines[i].Split(); // khai báo mảng number với giá trị là mảng lines theo thứ tự i
edgeList.AddLast(new Tuple<int, int, int>(int.Parse(numbers[0]), int.Parse(numbers[1]),
int.Parse(numbers[2])));// thêm vào danh sách cạnh từng giá trị đỉnh đầu , đỉnh cuối và weight
}
dist = new int[v, v]; // khai báo mảng dist giới hạn bởi số đỉnh v
pre = new int[v, v]; // khai báo mảng pre giới hạn bởi số đỉnh v
for (int i = 0; i < v; i++) //vòng lặp kiểm tra có cạnh giữa các đỉnh
{
for (int j = 0; j < v; j++)
{
dist[i, j] = INF;

}
}
foreach (Tuple<int, int, int> edge in edgeList)
{
// Đồ thị vô hướng
dist[edge.Item1 - 1, edge.Item2 - 1] = edge.Item3;
dist[edge.Item2 - 1, edge.Item1 - 1] = edge.Item3;
}
}
private void PrintDistMatrix() // hàm in ma trận có trọng số
{
for (int i = 0; i < v; i++)
{
for (int j = 0; j < v; j++)
{
if (dist[i, j] == INF)
{
Console.Write("\u221E ");// in ra ký hiệu vô cực
}
else
{
Console.Write(dist[i, j] + " ");
}
}
Console.WriteLine();
}
Console.WriteLine();
}
private string ExtractPath(int s, int t) // in kết quả vào file
{
if (pre[s - 1, t - 1] == 0)
{
return $" -> {t}";
}
return ExtractPath(s, pre[s - 1, t - 1]) + $" -> {t}";
}
public void Solve(string outputpath)// hàm giải quyết bài tập
{
// for more details
PrintDistMatrix();
for (int y = 0; y < v; y++)
{
for (int x = 0; x < v; x++)
{
if (dist[x, y] != INF) // nếu trọng số tại vị trị x,y khác INF
{
for (int j = 0; j < v; j++)
{
if (dist[y, j] != INF) // nếu trọng số tại vị trí y,j khác INF
{
if (dist[x, j] == INF || (dist[x, j] > dist[x, y] + dist[y, j])) // nếu trọng số [x,j] bằng INF hoặc trọng số
[x,j] > trọng số [x,y] + trọng số [y,j]
{
dist[x, j] = dist[x, y] + dist[y, j]; // cập nhật trọng số tại [x,j]
pre[x, j] = y + 1; // xác định giá trị pre
}
}
}
}
}
}
PrintDistMatrix();// in ma trận có trọng số đã được tìm đường đi ngắn nhất qua trung gian đỉnh x
int sDist = dist[s - 1, a - 1] + dist[a - 1, t - 1];
string sPath = $"{s}" + ExtractPath(s, a) + ExtractPath(a, t); // tổng hợp các giá trị vào biến string sPath
string[] data =
{sDist.ToString(),sPath}; // ép kiểu int sDist thành toString gán vào mảng string data
File.WriteAllLines(outputpath, data);// viết tất cả kết quả vào file
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lab07
{
internal class Ex3
{
private int INF = 9999;
private int v;
private int[,] dist;
public Ex3(string path)
{
if (!File.Exists(path)) // kiểm tra file có tồn tại không
{
throw new FileNotFoundException(path);
}
string[] lines = File.ReadAllLines(path);// khai báo mảng lines đọc tất cả dữ liệu file
v = int.Parse(lines[0]); // khai báo đỉnh v bằng giá trị đầu tiên của mảng lines
dist = new int[v, v]; // khai báo mảng trọng số được giới hạn bởi số đỉnh v
for (int i = 0; i < v; i++) // vòng lặp cập nhật giá trị của dist
{
string[] numbers = lines[i + 1].Split();
for (int j = 0; j < v; j++)
{
dist[i, j] = int.Parse(numbers[j]);
}
}
}
public void Solve(string outputPath) //hàm xử lý ex3
{

for (int y = 0; y < v; y++)


{
for (int x = 0; x < v; x++)
{
if (dist[x, y] != 0) // xét mảng dist khác 0
{
for (int j = 0; j < v; j++)
{
if (j == x) // nếu tại vị trí j == vị trị x thì bỏ qua
{
continue;
}
if (dist[y, j] != 0) // nếu trọng số tại vị trị [y,j] khác 0
{
if (dist[x, j] == 0 || (dist[x, j] > dist[x, y] + dist[y, j])) // nếu trọng số tại vị trí [x,j] bằng 0 hoặc trọng số
[x,j] lớn hơn trọng số [x,y] + dist[y,j]
{
dist[x, j] = dist[x, y] + dist[y, j]; // cập nhập lại trọng số ngắn nhất
}
}
}
}
}
}
PrintDistMatrix(); // in ma trận trọng số cập nhật trọng số ngắn nhất
LinkedList<string> data = new LinkedList<string>(); // khai báo danh sách data
data.AddLast(v.ToString()); // thêm giá trị vào danh sách ép kiểu string đỉnh v
for (int i = 0; i < v; i++)
{
string line = "";
for (int j = 0; j < v; j++)
{
line += $"{dist[i, j]} ";
}
data.AddLast(line);
}
File.WriteAllLines(outputPath, data); // viết tất cả dữ liệu vào file

}
private void PrintDistMatrix() // hàm in ma trận có trọng số
{
for (int i = 0; i < v; i++)
{
for (int j = 0; j < v; j++)
{
if (dist[i, j] == INF)
{
Console.Write("\u221E "); //in ra ký hiệu vô cực
}
else
{
Console.Write(dist[i, j] + " ");
}
}
Console.WriteLine();
}
Console.WriteLine();
}

}
}

You might also like