Domino Effect
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10454 | Accepted: 2590 |
Description
Did you know that you can use domino bones for other things besides playing Dominoes? Take a number of dominoes and build a row by standing them on end with only a small distance in between. If you do it right, you can tip the first domino and cause all others to fall down in succession (this is where the phrase ``domino effect'' comes from). 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 input file contains descriptions of several domino systems. The first line of each description contains two integers: the number n of key dominoes (1 <= n < 500) and the number m of rows between them. The key dominoes are numbered from 1 to n. There is at most one row between any pair of key dominoes and the domino graph is connected, i.e. there is at least one way to get from a domino to any other domino by following a series of domino rows. 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
For each case output a line stating the number of the case ('System #1', 'System #2', etc.). Then output a line containing the time when the last domino falls, exact to one digit to the right of the decimal point, and the location of the last domino falling, which is either at a key domino or between two key dominoes(in this case, output the two numbers in ascending order). Adhere to the format shown in the output sample. The test data will ensure there is only one solution. Output a blank line after each system.
Sample Input
2 11 2 273 31 2 51 3 52 3 50 0
Sample Output
System #1The last domino falls after 27.0 seconds, at key domino 2.System #2The last domino falls after 7.5 seconds, between key dominoes 2 and 3.
Source
题解:
这是一道最短路径题目,但是要判断哪种最优解的情况。设res为最后一张牌倒下的时刻,p1和p2记录的是最后倒下的关键牌的序号1。
a) 如果最后一张倒下的是关键牌。利用Dijkstra 算法求第1 张关键牌到其他每张关键牌的最短路径,保存在dis[i]。然后取dis[i]的最大值,设为res,p0..记录关键牌序号。
b) 如果最后一张倒下的是两张关键牌之间的普通牌。设该行两端的关键牌为i 和j,他们以每秒一个单位的速度相向而行,设i和j分别经过t1和t2秒相遇,i和j之间的距离为map[i][j],求i和j在什么时刻相遇。不难列出方程:t1+t2=map[i][j],dis[i]+map[i][j]=dis[j]+map[i][j]。则i和j相遇的时刻为t=(dis[i] + dis[j] +map[i][j])/2.0。res=min(res,t)。p1和p2记录两张关键牌的序号。(注意:要满足dis[i]+map[i][j]>dis[j]并且dis[j]+map[i][j]>dis[i]才应该计算t值)
c) 如果gard==1,则是a情况;否则是b情况(如果b情况成立,pos_j的值应该改变了)。
每一次AC背后都是无数次WA
对比两次的读入(只有读入不同)
WA代码
#include#include #include #include #include using namespace std;#define N 501#define inf INT_MAXint n,m,tot,map[N][N],vis[N],dis[N];int main(){ while(scanf("%d%d",&n,&m)==2&&n&&m){ memset(map,0,sizeof map); memset(vis,0,sizeof vis); memset(dis,0,sizeof dis); for(int i=1;i<=m;i++){ int u,v,w; scanf("%d%d%d",&u,&v,&w); map[u][v]=map[v][u]=w; } vis[1]=1; for(int i=1;i<=n;i++){ dis[i]=(map[1][i]!=0?map[1][i]:inf); } int t=1; dis[1]=0;//dijkstra for(int i=1;i dis[t]+map[t][j]){ dis[j]=dis[t]+map[t][j]; } } } int p0=1,p1=1,p2=1,gard=1;//开始找关键牌 double res=-inf; for(int i=1;i<=n;i++){ if(res res){ gard=0; res=x1; p1=i; p2=j; } } } printf("System #%d\n", ++tot); if(gard==1){ printf("The last domino falls after %.1lf seconds, at key domino %d.\n", res, p0); } else{ if(p1>p2) swap(p1,p2); printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n", res, p1, p2); } putchar('\n'); } return 0;}
AC代码
#include#include #include #include #include using namespace std;#define N 501#define inf INT_MAXint n,m,tot,map[N][N],vis[N],dis[N];int main(){ scanf("%d%d",&n,&m); for(;n+m;){ memset(map,0,sizeof map); memset(vis,0,sizeof vis); memset(dis,0,sizeof dis); for(int i=1;i<=m;i++){ int u,v,w; scanf("%d%d%d",&u,&v,&w); map[u][v]=map[v][u]=w; } vis[1]=1; for(int i=1;i<=n;i++){ dis[i]=(map[1][i]!=0?map[1][i]:inf); } int t=1; dis[1]=0;//dijkstra for(int i=1;i dis[t]+map[t][j]){ dis[j]=dis[t]+map[t][j]; } } } int p0=1,p1=1,p2=1,gard=1;//开始找关键牌 double res=-inf; for(int i=1;i<=n;i++){ if(res res){ gard=0; res=x1; p1=i; p2=j; } } } printf("System #%d\n", ++tot); if(gard==1){ printf("The last domino falls after %.1lf seconds, at key domino %d.\n", res, p0); } else{ if(p1>p2) swap(p1,p2); printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n", res, p1, p2); } putchar('\n'); scanf("%d%d",&n,&m); } return 0;}
我醉了~~tyts