附录:(注:所有程序均使用Microsoft Visual Studio 2005 C#语言编写) 程序一(第一题优化转乘次数程序):
using System; using System.IO;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text;
using System.Windows.Forms; using System.Globalization; using System.Collections;
using System.Windows.Forms.Design;
namespace model {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
public struct line {
public char price; public char direct; public int[,] stop; }
public static int count = 0, goal = 2, money = 99, distance = 9999; public static int[] route = new int[10];
public static int[,] mon = new int[3958, 3958];
public void loop(int x, int y) {
distance = 9999;
if (dis[x, y] < distance) {
distance = dis[x, y]; goal = count;
31
}
count++;
if (count > 2) {
count --; return; }
for (int i = 1; i < 3958; i++) {
if (dis[x, i] + dis[i, y] + 10 < distance) {
distance = dis[x, i] + dis[i, y] + 10; route[count] = i; goal = count; } }
count++;
if (count > 2) {
count -= 2; return; }
for (int i = 1; i < 3958; i++) {
for (int j = 1; j < 3958; j++) {
if (dis[x, i] != 9999 && dis[j, y] != 9999 && i != j) {
if (dis[x, i] + dis[j, y] + 20 + dis[i,j] < distance) {
distance = dis[x, i] + dis[j, y] + 20 + dis[i,j]; route[count] = i; route[count + 1] = j; goal = count; } } }
32
}
count -= 2; return; }
private void Form1_Load(object sender, EventArgs e) {
FileStream info = new FileStream(\竞赛\\\\info.txt\FileAccess.ReadWrite),
infor = new FileStream(\竞赛\\\\infor.txt\FileMode.Open, FileAccess.ReadWrite);
StreamReader sr = new StreamReader(infor);
int flag = 0;
string a = string.Empty; line[] city = new line[521];
a = sr.ReadToEnd(); sr.Close(); infor.Close();
for (int i = 0, j = 0, h = 0, k = 0; i < a.Length; i++) {
switch (flag) {
case 0: {
if (a[i] == 'S') {
city[j].stop[k, h] = Convert.ToInt32(a.Substring(i + 1, 4));
h++; }
else if (a[i] == 'D') {
k = 1; h = 0; flag = 0; }
else if (a[i] == 'L') {
if (k == 0)
33
{
if (city[j].direct == 'S')
for (int scan = 0; scan < h; scan++) {
city[j].stop[1, scan] = city[j].stop[0, h - 1 - scan];
} else
city[j].stop[0, scan];
for (int scan = 0; scan < h; scan++) {
city[j].stop[0, scan + h - 1] = } } j++;
city[j].stop = new int[2, 170]; flag = 1; }
break; } case 1: {
if (a[i] == 'M') {
city[j].price = 'M'; flag = 2; }
else if (a[i] == 'F') {
city[j].price = 'F'; flag = 2; }
break; } case 2: {
if (a[i] == 'U') {
city[j].direct = 'U'; k = 0; h = 0; flag = 0; }
34
else if (a[i] == 'C') {
city[j].direct = 'C'; k = 0; h = 0; flag = 0; }
else if (a[i] == 'S') {
city[j].direct = 'S'; k = 0; h = 0;
city[j].stop[k, h] = Convert.ToInt32(a.Substring(i + 1, 4));
h++; flag = 0; }
break; } } }
for (int x = 0; x < 3958; x++) for (int y = 0; y < 3958; y++) dis[x, y] = 9999;
for (int j = 1; j <= 520; j++) {
for (int x = 0; city[j].stop[0, x] > 0; x++) for (int y = x; city[j].stop[0, y] > 0; y++) {
if (dis[city[j].stop[0, x], city[j].stop[0, y]] > (y - x) * 3) dis[city[j].stop[0, x], city[j].stop[0, y]] = (y - x) * 3;
}
for (int x = 0; city[j].stop[1, x] > 0; x++) for (int y = x; city[j].stop[1, y] > 0; y++) {
if (dis[city[j].stop[1, x], city[j].stop[1, y]] > (y - x) * 3) dis[city[j].stop[1, x], city[j].stop[1, y]] = (y - x) * 3;
} }
35