2013
11-09

Domino Effect

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.

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.

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.

2 1
1 2 27
3 3
1 2 5
1 3 5
2 3 5
0 0

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.

//* @author: [email protected]/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */
import java.util.Scanner;
public class Main
{
static final int Inf=9999999;
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int cnt=0;
while(true)
{
cnt++;
int n=in.nextInt();
int m=in.nextInt();
if(n==0&&m==0)break;
int[][] p=new int[n][n];
for(int i=0;i< n;i++)
for(int j=0;j< n;j++)
p[i][j]=Inf;
for(int i=0;i < m;i++)
{
int x=in.nextInt()-1;
int y=in.nextInt()-1;
int len=in.nextInt();
p[x][y]=p[y][x]=len;
}
boolean[] bb=new boolean[n];
bb[0]=true;
int[] time=new int[n];
for(int i=0;i< n;i++)
time[i]=p[0][i];
time[0]=0;
for(int i=0;i< n-1;i++)
{
int min=Inf,tag=0;
for(int j=0;j< n;j++)
{
if(!bb[j]&&time[j]< min)
{
min=time[j];
tag=j;
}
}
bb[tag]=true;
for(int j=0;j< n;j++)
if(!bb[j]&&p[tag][j]< Inf&&time[tag]+p[tag][j]< time[j])
time[j]=time[tag]+p[tag][j];
}
double maxtime=-Inf;
int pos=-1;
for(int i=0;i< n;i++)
{
if(time[i]>maxtime)
{
maxtime=time[i];
pos=i;
}
}
double max=-Inf,t;
int pos1=-1,pos2=-1;
for(int i=0;i< n;i++)
{
for(int j=i+1;j< n;j++)
{
t=(time[i]+time[j]+p[i][j])/2.0;
if(p[i][j]< Inf&&t>max)
{
max=t;
pos1=i;pos2=j;
}
}
}
System.out.println("System #"+cnt);
System.out.print("The last domino falls after ");
if(maxtime>=max)
System.out.printf("%.1f seconds, at key domino %d.\n\n",maxtime,pos+1);
else System.out.printf("%.1f seconds, between key dominoes %d and %d.\n\n",max,pos1+1,pos2+1);
}
}
}

1. 为什么for循环找到的i一定是素数叻，而且约数定理说的是n=p1^a1*p2^a2*p3^a3*…*pk^ak，而你每次取余都用的是原来的m，也就是n

2. 约瑟夫也用说这么长……很成熟的一个问题了，分治的方法解起来o(n)就可以了，有兴趣可以看看具体数学的第一章，关于约瑟夫问题推导出了一系列的结论，很漂亮