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 |
一直Runtime Error,也不知道为啥?求大佬解答#include<cstdio> #include<cstring> #include<vector> #include<queue> #include<algorithm> using namespace std; const int maxn=1e4+7; struct edge { int to, cost; }; vector<edge> g[maxn]; queue<int> que; int vis[maxn]; int dis[maxn]; int bfs(int s, int flag) { memset(vis, 0, sizeof(vis)); memset(dis, 0, sizeof(dis)); edge e; int tmp=0, tmpi, u; vis[s]=1; dis[s]=0; que.push(s); while(!que.empty()) { u=que.front(); que.pop(); for(int i=0; i<g[u].size(); i++) { e=g[u][i]; if(vis[e.to]==0) { vis[e.to]=1; dis[e.to]=dis[u]+e.cost; if(tmp < dis[e.to]) { tmp=dis[e.to]; tmpi=e.to; } que.push(e.to); } } } if(flag==0) return tmpi; else return tmp; } int main() { int a, b, c, ans=0; edge e; while(scanf("%d%d%d", &a, &b, &c)!=EOF) { e.cost=c; e.to=b; g[a].push_back(e); e.to=a; g[b].push_back(e); } a=bfs(1, 0); //这里任意选了一个点 ans=bfs(a, 1); printf("%d\n", ans); return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator