Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
老是WA. 我觉得 没什么问题啊! 请各位大侠帮忙看看~#include <iostream> using namespace std; struct node{ int v; //村庄编号 node *pNext; }; class Problem{ int arr[101][101]; node inList; //已经联通的村庄 node outList; //未联通的村庄 int n; //村庄数 long length; //目标最短路径 public: Problem(int n) { this->n = n; length = 0; inList.pNext = NULL; outList.pNext = NULL; } void getMatrix()//读取矩阵 { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> arr[i][j]; } void initInList() //根据Q个行来初始化已经联通的村庄,和未联通的村庄 { int q = 0; int* isInList = new int[n + 1]; //N个村庄的联通情况 for (int i = 1; i <= n; i++) isInList[i] = 0; cin >> q; for (int j = 0; j < 2 * q; j++) { int v = 0; cin >> v; isInList[v] = 1; } node *tmp1 = &inList; node *tmp2 = &outList; for (int s = 1; s <= n; s++) { node *tmp = new node; tmp->v = s; tmp->pNext = NULL; if (1 == isInList[s]) { tmp1->pNext = tmp; tmp1 = tmp; } else { tmp2->pNext = tmp; tmp2 = tmp; } } } void getLong() //将未联通的村庄一个个添加到联通的集里 { while(outList.pNext) { node *perfect = outList.pNext; int value = 0; node *in = inList.pNext; if (in) value = arr[in->v][perfect->v]; while (in) { node *out = outList.pNext; while (out) { if (arr[in->v][out->v] < value) { perfect = out; value = arr[in->v][out->v]; } out = out->pNext; } in = in->pNext; } node *tmp = &outList; while (tmp->pNext != perfect) { tmp = tmp->pNext; } tmp->pNext = perfect->pNext; perfect->pNext = inList.pNext; inList.pNext = perfect; length += value; } } void go() { getMatrix(); initInList(); getLong(); cout << length << endl; } }; int _tmain(int argc, _TCHAR* argv[]) { int n = 0; cin >> n; Problem p(n); p.go(); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator