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> #include <stdio.h> using namespace std; int main() { int cases; scanf("%d", &cases); for(int ii = 0; ii < cases; ii++){ int adjList[104][104]; int adjDist[104][104]; int adjNum[104] = {0}; int dist[104]; int n, m; scanf("%d%d", &n, &m); for(int i = 0; i < m; i++){ int x, y, d; scanf("%d%d%d", &x, &y, &d); if(x == y) continue; adjList[x][adjNum[x]] = y; adjDist[x][adjNum[x]] = d; adjList[y][adjNum[y]] = x; adjDist[y][adjNum[y]] = d; adjNum[x] ++; adjNum[y] ++; } bool used[104] = {0,1,0}; int from[104]; dist[1] = 0; for(int i = 2; i <= n; i++){ dist[i] = 2147483647; from[i] = -1; } for(int i = 0; i < adjNum[1]; i++){ dist[adjList[1][i]] = adjDist[1][i]; from[adjList[1][i]] = 1; } int cnt = 1; int cost = 0; while(cnt < n){ int mn = 2147483647; int arg = -1; for(int i = 2; i <= n; i++){ if(used[i]) continue; if(dist[i] < mn){ mn = dist[i]; arg = i; } } used[arg] = 1; cnt ++; cost += dist[arg]; //cout << cost << endl; for(int i = 0; i < adjNum[arg]; i++){ if(used[adjList[arg][i]]) continue; if(adjDist[arg][i] < dist[adjList[arg][i]]){ dist[adjList[arg][i]] = adjDist[arg][i]; from[adjList[arg][i]] = arg; } } } //cout << cost << endl; bool bwy = 0; for(int TDi = 2; TDi <= n; TDi ++){ int TDj = from[TDi]; //克掉TDi到TDj的这条边 bool Used[104] = {0,1,0}; int Dist[104]; Dist[1] = 0; for(int i = 2; i <= n; i++){ Dist[i] = 2147483647; } for(int i = 0; i < adjNum[1]; i++){ if(TDj == 1 && TDi == adjList[1][i]) continue; Dist[adjList[1][i]] = adjDist[1][i]; } int Cnt = 1; int Cost = 0; //bool Bwy = 0; while(Cnt < n){ int Mn = 2147483647; int Arg = -1; for(int i = 2; i <= n; i++){ if(Used[i]) continue; if(Dist[i] < Mn){ Mn = Dist[i]; Arg = i; } } if(Mn == 2147483647){ Cost = -1; break; } Used[Arg] = 1; Cnt ++; Cost += Dist[Arg]; //cout << cost << endl; for(int i = 0; i < adjNum[Arg]; i++){ if(Used[adjList[Arg][i]]) continue; if(Arg == TDi && adjList[Arg][i] == TDj) continue; if(Arg == TDj && adjList[Arg][i] == TDi) continue; if(adjDist[Arg][i] < Dist[adjList[Arg][i]]){ Dist[adjList[Arg][i]] = adjDist[Arg][i]; } } } if(Cost == cost){ bwy = 1; break; } } if(bwy){ cout << "Not Unique!" << endl; } else cout << cost << endl; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator