Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6322 | Accepted: 1609 |
Description
While this is somewhat pointless with only a few dominoes, some people went to the opposite extreme in the early Eighties. Using millions of dominoes of different colors and materials to fill whole halls with elaborate patterns of falling dominoes, they created (short-lived) pieces of art. In these constructions, usually not only one but several rows of dominoes were falling at the same time. As you can imagine, timing is an essential factor here.
It is now your task to write a program that, given such a system of rows formed by dominoes, computes when and where the last domino falls. The system consists of several ``key dominoes'' connected by rows of simple dominoes. When a key domino falls, all rows connected to the domino will also start falling (except for the ones that have already fallen). When the falling rows reach other key dominoes that have not fallen yet, these other key dominoes will fall as well and set off the rows connected to them. Domino rows may start collapsing at either end. It is even possible that a row is collapsing on both ends, in which case the last domino falling in that row is somewhere between its key dominoes. You can assume that rows fall at a uniform rate.
Input
The following m lines each contain three integers a, b, and l, stating that there is a row between key dominoes a and b that takes l seconds to fall down from end to end.
Each system is started by tipping over key domino number 1.
The file ends with an empty system (with n = m = 0), which should not be processed.
Output
Sample Input
2 1 1 2 27 3 3 1 2 5 1 3 5 2 3 5 0 0
Sample Output
System #1 The last domino falls after 27.0 seconds, at key domino 2. System #2 The last domino falls after 7.5 seconds, between key dominoes 2 and 3.
Source
思路:求出1到每个点的最短路,然后在看看骨牌是否会倒在两个点的中间。
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; const int mm=510; const int oo=1e9; float grap[mm][mm],dis[mm]; bool vis[mm]; int m,n; void dij() { memset(vis,0,sizeof(vis)); for(int i=0;i<=m;i++) dis[i]=grap[1][i]; dis[1]=0.0;vis[1]=1; int _min=oo,u; for(int i=1;i<m;i++) { u=0;_min=oo; for(int j=1;j<=m;j++) if(!vis[j]&&dis[j]<_min) _min=dis[j],u=j; vis[u]=1; for(int j=1;j<=m;j++) if(!vis[j]&&dis[j]>dis[u]+grap[u][j]) dis[j]=dis[u]+grap[u][j]; } } int main() { int cas=0; while(cin>>m>>n) { memset(grap,0,sizeof(grap)); if(m==0&&n==0)break; int a,b,c; for(int i=0;i<=m;i++) for(int j=0;j<=m;j++) grap[i][j]=oo; for(int i=0;i<n;i++) { cin>>a>>b>>c; grap[a][b]=grap[b][a]=c; } dij(); float ans1=-oo,ans2=-oo; for(int i=1;i<=m;i++) if(ans1<dis[i]) ans1=dis[i],c=i; for(int i=1;i<=m;i++) for(int j=i+1;j<=m;j++) { float z=(dis[i]+dis[j]+grap[i][j])/2.0;///判断两点之间 if(grap[i][j]<oo&&z>ans2) { a=i;b=j;ans2=z; } }++cas; cout<<"System #"<<cas<<"\n"; if(ans2>ans1) printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n\n",ans2,a,b); else printf("The last domino falls after %.1f seconds, at key domino %d.\n\n",ans1,c); } }