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 |
最短路+枚举每一条边的情况 附AC代码,之前WA了n次,最后发现没有对mp数组memset#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<cmath> #include<queue> using namespace std; typedef long long LL; const double pi=acos(-1.0); int n,m; vector<int> p[501]; int mp[501][501]; int dis[501]; bool inque[501]; void bfs() { memset(dis,0x38,sizeof dis); dis[1]=0; queue<int> q; q.push(1); inque[1]=true; while(!q.empty()) { int x=q.front(); q.pop(); inque[x]=false; for(int i=0;i<p[x].size();i++) { int y=p[x][i]; if(dis[y]>dis[x]+mp[x][y]) { dis[y]=dis[x]+mp[x][y]; if(!inque[y]) { inque[y]=true; q.push(y); } } } } double ans=0; int mi=1,mj=0; for(int i=1;i<n;i++) { for(int j=i+1;j<=n;j++) { if(mp[i][j]==0) continue; if(abs(dis[i]-dis[j])>=mp[i][j]) { if(max(dis[i],dis[j])>ans) { ans=max(dis[i],dis[j]); if(dis[i]>dis[j]) { mi=i; } else { mi=j; } mj=0; } } else { int cha=max(dis[i],dis[j])-min(dis[i],dis[j]); double time=min(dis[i],dis[j])+cha; time+=(mp[i][j]-cha)/2.0; if(time>ans) { ans=time; mi=i; mj=j; } } } } if(mj) { printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n",ans,mi,mj); } else { printf("The last domino falls after %.1f seconds, at key domino %d.\n",ans,mi); } } int main() { freopen("input.txt","r",stdin); int tc=0; for(;;) { tc++; scanf("%d%d",&n,&m); if(!n) break; for(int i=1;i<=n;i++) p[i].clear(); memset(mp,0,sizeof mp); while(m--) { int a,b,c; scanf("%d%d%d",&a,&b,&c); p[a].push_back(b); p[b].push_back(a); mp[a][b]=c; mp[b][a]=c; } printf("System #%d\n",tc); bfs(); printf("\n"); } fclose(stdin); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator