Domino Effect POJ - 1135 题解
2017-02-19 12:08
375 查看
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 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.
个人觉得这道题很有意思,有关键骨牌和普通骨牌,推倒第一块骨牌,求最后倒掉的骨牌的位置和时间,抽象一下就是单源最短路距离起点最远的地方,方法就是先DIJ求每个关键骨牌到起点的最短路,然后枚举每条边求最远的地方,对于e[i]以及其两端的a和b(dis[a]小于dis[b])若,dis[b]-dis[a]==e[i],即骨牌是从a倒到b的,若dis[b]是路径最长的地方,则最后一块倒掉的是关键骨牌b,若e大于a到b的dis差,则最后倒掉的地方在e上,则需要计算sec = double(len[j] - (dis[b] - dis[a])) / 2.0 + dis[b];
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 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.
个人觉得这道题很有意思,有关键骨牌和普通骨牌,推倒第一块骨牌,求最后倒掉的骨牌的位置和时间,抽象一下就是单源最短路距离起点最远的地方,方法就是先DIJ求每个关键骨牌到起点的最短路,然后枚举每条边求最远的地方,对于e[i]以及其两端的a和b(dis[a]小于dis[b])若,dis[b]-dis[a]==e[i],即骨牌是从a倒到b的,若dis[b]是路径最长的地方,则最后一块倒掉的是关键骨牌b,若e大于a到b的dis差,则最后倒掉的地方在e上,则需要计算sec = double(len[j] - (dis[b] - dis[a])) / 2.0 + dis[b];
#include<iostream> #include<string.h> #include<stdio.h> #include<string> #include<vector> #include<algorithm> #include<queue> using namespace std; int n, m; int head[505], nex[250055], to[250055], len[250055]; int dis[505]; bool v[505]; bool vis[250055]; int ne; void add(int x, int y, int z){ to[ne] = y; len[ne] = z; nex[ne] = head[x]; head[x] = ne++; } struct point{ int n,step; bool operator < (const point& b)const{ return step>b.step; } }; priority_queue<point> que; void dij(){ while (!que.empty()){ int cnt = que.top().n; que.pop(); v[cnt] = 1; for (int i = head[cnt]; i != -1; i = nex[i]){ int u = to[i],l=len[i]; if (!v[u] && dis[u] > dis[cnt] + l){ dis[u] = dis[cnt] + l; que.push({ u, dis[u] }); } } } } int main(){ int x, y, z; int t = 0; while (scanf("%d%d", &n, &m),n+m){ t++; ne = 0; for (int i = 1; i <= n; i++)head[i] = -1; for (int i = 0; i < m; i++){ scanf("%d%d%d", &x, &y, &z); add(x, y, z); add(y, x, z); } memset(dis, 0x3f, sizeof(dis)); dis[1] = 0; memset(v, 0, sizeof(v)); que.push({ 1, 0 }); dij(); memset(vis, 0, sizeof(vis)); float ans = 0; int ansa=1, ansb=1; for (int i = 1; i <= n; i++){ for (int j = head[i]; j != -1; j = nex[j]){ if (vis[j] == 0){ vis[j] = 1; vis[j ^ 1] = 1; int a = i, b = to[j]; if (dis[a] > dis[b]){ swap(a, b); } double sec; if (dis[b] - dis[a] == len[j]){ sec = dis[b]; if (sec>ans){ ans = sec; ansa = ansb = b; } } else{ sec = double(len[j] - (dis[b] - dis[a])) / 2.0 + dis[b]; if (sec > ans){ ans = sec; ansa = a, ansb = b; } } } } } if (t != 1)puts(""); printf("System #%d\n", t); if (ansa == ansb){ printf("The last domino falls after %.1f seconds, at key domino %d.\n", ans, ansa); } else{ if (ansa > ansb)swap(ansa, ansb); printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n", ans, ansa, ansb); } } return 0; }
相关文章推荐
- TOJ 1883 ZOJ 1298 POJ 1135 Domino Effect / Dijkstra
- G - Domino Effect POJ - 1135
- poj&nbsp;1135&nbsp;Domino&nbsp;Effect(dij)
- poj1135 - Domino Effect
- poj&nbsp;1135&nbsp;Domino&nbsp;Effect&nbsp;单源最短…
- Domino Effect ZOJ 1298 POJ 1135 Dijkstra
- poj 1135 最短路 dijkstra
- POJ 1135/ZOJ 1298 Domino Effect (最短路径dijkstra)
- POJ 1135 Domino Effect(最短路 多米诺骨牌)
- POJ 1135 Domino Effect (spfa + 枚举)- from lanshui_Yang
- poj 1135 Domino Effect(最短路+枚举)
- CodeForces - 405B(Domino Effect)
- poj 1135 Domino Effect
- UVA 211 The Domino Effect 多米诺效应 (回溯)
- 最短路径 POJ 1135 Domino Effect
- POJ1135 Domino Effect(SPFA)
- The Domino Effect UVA - 211
- The Domino Effect UVA - 211
- Uva211 The Domino Effect 【dfs回溯】【习题7-3】
- POJ 3408 The Domino Principle 可能会