Problem Description
Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst the stockbrokers to give your employer the tactical edge in the stock market. For maximum effect, you have to spread the rumours in the fastest possible way.
Unfortunately for you, stockbrokers only trust information coming from their “Trusted sources” This means you have to take into account the structure of their contacts when starting a rumour. It takes a certain amount of time for a specific stockbroker to pass the rumour on to each of his colleagues. Your task will be to write a program that tells you which stockbroker to choose as your starting point for the rumour, as well as the time it will take for the rumour to spread throughout the stockbroker community. This duration is measured as the time needed for the last person to receive the information.
Input
Your program will input data for different sets of stockbrokers. Each set starts with a line with the number of stockbrokers. Following this is a line for each stockbroker which contains the number of people who they have contact with, who these people are, and the time taken for them to pass the message to each person. The format of each stockbroker line is as follows: The line starts with the number of contacts (n), followed by n pairs of integers, one pair for each contact. Each pair lists first a number referring to the contact (e.g. a ‘1’ means person number one in the set), followed by the time in minutes taken to pass a message to that person. There are no special punctuation symbols or spacing rules.
Each person is numbered 1 through to the number of stockbrokers. The time taken to pass the message on will be between 1 and 10 minutes (inclusive), and the number of contacts will range between 0 and one less than the number of stockbrokers. The number of stockbrokers will range from 1 to 100. The input is terminated by a set of stockbrokers containing 0 (zero) people.
Output
For each set of data, your program must output a single line containing the person who results in the fastest message transmission, and how long before the last person will receive any given message after you give it to this person, measured in integer minutes.
It is possible that your program will receive a network of connections that excludes some persons, i.e. some people may be unreachable. If your program detects such a broken network, simply output the message “disjoint”. Note that the time taken to pass the message from person A to person B is not necessarily the same as the time taken to pass it from B to A, if such transmission is possible at all.
题意:
题目可以大致抽象为给出一系列的点,这些点组成一个连通图,求这些点中能够连同其余各个点的,并且路径是最短的的一个点以及最短路径长度。
思路:
(求最短路径问题)
由于题目没有给出初始点,所以我用Dijkstra算法分别求出各点到其余点的最短路径,其中路径最长的就是该点到其余各点的最短路径,把它按序储存在一个数组中,最后从这个数组中选择最小值,数组下标对应起始点,值就是最短路径长度。
import java.util.Scanner;
public class MainA1000 {
// class person{
// contact ct[];
//
// public person(contact[] c) {
// // TODO 自动生成的构造函数存根
// ct = c;
// }
// }
//
// class contact{
// int person;
// int time;
//
// public contact(int c,int t) {
// // TODO 自动生成的构造函数存根
// person = c;
// time = t;
// }
// }
class graph{
int [][] adj;
Path dist[];
public graph(int n) {
// TODO 自动生成的构造函数存根
adj = new int [n][n];
for(int i= 0;i<n;i++){
for(int j=0;j<n;j++){
if(i!=j){
adj[i][j] = Integer.MAX_VALUE;
}else{
adj[i][j] = 0;
}
}
}
}
void setV0(int v0){
dist[v0].length = 0;
dist[v0].prevex = 0;
adj[v0][v0] = 1;
}
}
public static void main(String[] args) {
MainA1000 demo = new MainA1000();
Scanner scanner = new Scanner(System.in);
int N; // Stockbrokers数量
int n; // number of contacts
// person [] stockbroker;
// contact[] con;
graph g;
// Path p;
int i=0,j=0;
int to,length ;
int mv,minw;
int max[];
int min;
int minnum;
while(scanner.hasNext()){
N = scanner.nextInt();
if(N == 0){
scanner.close();
break;
}else{
// stockbroker = new person[N]; //数据输入,stockbroker数组和con数组
// for(i=0;i<N;i++){
// n = scanner.nextInt();
// con = new contact[n];
// for(j=0;j<n;j++){
// per = scanner.nextInt();
// t = scanner.nextInt();
// con[j] = demo.new contact(per, t);
// }
// stockbroker[i] = demo.new person(con);
// }
g = demo .new graph(N);
g.dist = new Path[N];
max = new int[N];
min = Integer.MAX_VALUE;
minnum = 0;
for(i=0;i<N;i++){
n = scanner.nextInt();
if(n!=0){
for(j=0;j<n;j++){
to = scanner.nextInt();
length = scanner.nextInt();
g.adj[i][to-1] = length;
}
}
}
for(i=0;i<N;i++){
graph gdemo = demo.new graph(N);
for(int x=0;x<N;x++){
for(int y=0;y<N;y++){
gdemo.adj[x][y] = g.adj[x][y];
}
}
for(j=0;j<N;j++){
gdemo.dist[j] = new Path();
}
gdemo.setV0(i);
for(j=0;j<N;j++){
gdemo.dist[j].length = gdemo.adj[i][j];
if(gdemo.dist[j].length!=Integer.MAX_VALUE){
gdemo.dist[j].prevex = i;
}else{
gdemo.dist[j].prevex = -1;
}
}
for(int k=0;k<N;k++){
minw = Integer.MAX_VALUE;
mv = -1;
for(j=0;j<N;j++ ){
if(gdemo.adj[j][j]==0&&gdemo.dist[j].length<minw){
mv = j;
minw = gdemo.dist[j].length;
}
}
if(mv==-1){
break;
}
gdemo.adj[mv][mv] = 1;
for(j=0;j<N;j++){
if(gdemo.adj[j][j]==0&&gdemo.dist[j].length>gdemo.dist[mv].length+gdemo.adj[mv][j]){
if(gdemo.dist[mv].length+gdemo.adj[mv][j]>0){
gdemo.dist[j].prevex = mv;
gdemo.dist[j].length = gdemo.dist[mv].length + gdemo.adj[mv][j];
}
}
}
}
for(j=0;j<N;j++){
if(max[i]<gdemo.dist[j].length){
max[i] = gdemo.dist[j].length;
}
}
}
for(i=0;i<N;i++){
if(min>max[i]){
min = max[i];
minnum = i;
}
}
System.out.println(minnum+1+" "+ min);
}
}
}
}
class Path{
int length;
int prevex;
public Path(int len,int pre) {
// TODO 自动生成的构造函数存根
length = len;
prevex = pre;
}
public Path() {
// TODO 自动生成的构造函数存根
}
}