MPI Maelstrom
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7471   Accepted: 4550

Description

BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKee's research advisor, Jack Swigert, has asked her to benchmark the new system. 
``Since the Apollo is a distributed shared memory machine, memory access and communication times are not uniform,'' Valentine told Swigert. ``Communication is fast between processors that share the same memory subsystem, but it is slower between processors that are not on the same subsystem. Communication between the Apollo and machines in our lab is slower yet.''

``How is Apollo's port of the Message Passing Interface (MPI) working out?'' Swigert asked.

``Not so well,'' Valentine replied. ``To do a broadcast of a message from one processor to all the other n-1 processors, they just do a sequence of n-1 sends. That really serializes things and kills the performance.''

``Is there anything you can do to fix that?''

``Yes,'' smiled Valentine. ``There is. Once the first processor has sent the message to another, those two can then send messages to two other hosts at the same time. Then there will be four hosts that can send, and so on.''

``Ah, so you can do the broadcast as a binary tree!''

``Not really a binary tree -- there are some particular features of our network that we should exploit. The interface cards we have allow each processor to simultaneously send messages to any number of the other processors connected to it. However, the messages don't necessarily arrive at the destinations at the same time -- there is a communication cost involved. In general, we need to take into account the communication costs for each link in our network topologies and plan accordingly to minimize the total time required to do a broadcast.''

Input

The input will describe the topology of a network connecting n processors. The first line of the input will be n, the number of processors, such that 1 <= n <= 100.

The rest of the input defines an adjacency matrix, A. The adjacency matrix is square and of size n x n. Each of its entries will be either an integer or the character x. The value of A(i,j) indicates the expense of sending a message directly from node i to node j. A value of x for A(i,j) indicates that a message cannot be sent directly from node i to node j.

Note that for a node to send a message to itself does not require network communication, so A(i,i) = 0 for 1 <= i <= n. Also, you may assume that the network is undirected (messages can go in either direction with equal overhead), so that A(i,j) = A(j,i). Thus only the entries on the (strictly) lower triangular portion of A will be supplied.

The input to your program will be the lower triangular section of A. That is, the second line of input will contain one entry, A(2,1). The next line will contain two entries, A(3,1) and A(3,2), and so on.

Output

Your program should output the minimum communication time required to broadcast a message from the first processor to all the other processors.

Sample Input

  1. 5
  2. 50
  3. 30 5
  4. 100 20 50
  5. 10 x x 10

Sample Output

  1. 35
    题意:给出结点数,用邻接矩阵给出每个节点的距离,因为结点i到结点i的距离为0以及路径是双向的所以只给出邻接矩阵对角线的左下半块。
    下面分别用求最短路径的方法实现
  1. /*
  2. dijkstra 1502 Accepted 428K 0MS G++
  3. */
  4. #include"cstdio"
  5. #include"cstring"
  6. #include"algorithm"
  7. using namespace std;
  8. const int MAXN=;
  9. const int INF=0x3fffffff;
  10. int mp[MAXN][MAXN];
  11. int V;
  12. int dijkstra(int s)
  13. {
  14. int d[MAXN];
  15. int vis[MAXN];
  16. for(int i=;i<=V;i++)
  17. {
  18. vis[i]=;
  19. d[i]=mp[s][i];
  20. }
  21.  
  22. int n=V;
  23. while(n--)
  24. {
  25. int mincost,k;
  26. mincost=INF;
  27. for(int i=;i<=V;i++)
  28. {
  29. if(!vis[i]&&mincost>d[i])
  30. {
  31. mincost=d[i];
  32. k=i;
  33. }
  34. }
  35.  
  36. vis[k]=;
  37. for(int i=;i<=V;i++)
  38. {
  39. if(!vis[i]&&d[i]>d[k]+mp[k][i])
  40. {
  41. d[i]=d[k]+mp[k][i];
  42. }
  43. }
  44. }
  45.  
  46. int ans=-;
  47. for(int i=;i<=V;i++) ans=max(ans,d[i]);
  48. return ans;
  49. }
  50. int main()
  51. {
  52. while(scanf("%d",&V)!=EOF)
  53. {
  54. for(int i=;i<=V;i++)
  55. for(int j=;j<=i;j++)
  56. if(i==j) mp[i][j]=;
  57. else{
  58. char x[];
  59. scanf("%s",x);
  60. if(x[]=='x') mp[i][j]=mp[j][i]=INF;
  61. else mp[i][j]=mp[j][i]=atoi(x);
  62. }
  63.  
  64. printf("%d\n",dijkstra());
  65. }
  66. return ;
  67. }
  1. /*
  2. 堆优化dijkstra 1502 1502 Accepted 632K 0MS G++
  3. */
  4. #include"cstdio"
  5. #include"cstring"
  6. #include"algorithm"
  7. #include"vector"
  8. #include"queue"
  9. using namespace std;
  10. const int MAXN=;
  11. const int INF=0X3fffffff;
  12. struct Edge{
  13. int to,cost;
  14. Edge(){}
  15. Edge(int to,int cost)
  16. {
  17. this->to=to;
  18. this->cost=cost;
  19. }
  20. friend bool operator<(const Edge &a,const Edge &b)
  21. {
  22. return a.cost < b.cost;
  23. }
  24. };
  25. vector<Edge> G[MAXN];
  26. int V;
  27. int dijkstra(int s)
  28. {
  29. int d[MAXN];
  30. for(int i=;i<=MAXN;i++) d[i]=INF;
  31. d[s]=;
  32.  
  33. priority_queue<Edge> que;
  34. que.push(Edge(s,));
  35. while(!que.empty())
  36. {
  37. Edge e=que.top();que.pop();
  38. int v=e.to;
  39. if(d[v]<e.cost) continue;
  40. for(int i=;i<G[v].size();i++)
  41. {
  42. Edge ek=G[v][i];
  43. if(d[ek.to]>d[v]+ek.cost)
  44. {
  45. d[ek.to]=d[v]+ek.cost;
  46. que.push(Edge(ek.to,d[ek.to]));
  47. }
  48. }
  49. }
  50. int ans=-;
  51. for(int i=;i<=V;i++)
  52. if(d[i]<INF)
  53. ans=max(ans,d[i]);
  54. return ans;
  55. }
  56. int main()
  57. {
  58. while(scanf("%d",&V)!=EOF)
  59. {
  60. for(int i=;i<=V;i++)
  61. G[i].clear();
  62. for(int i=;i<=V;i++)
  63. for(int j=;j<=i;j++)
  64. if(i==j){
  65. G[i].push_back(Edge(j,));
  66. G[j].push_back(Edge(i,));
  67. }
  68. else{
  69. char x[];
  70. scanf("%s",x);
  71. if(x[]=='x') ;
  72. else{
  73. G[j].push_back(Edge(i,atoi(x)));
  74. G[i].push_back(Edge(j,atoi(x)));
  75. }
  76. }
  77. printf("%d\n",dijkstra());
  78. }
  79. return ;
  80. }
  1. /*
  2. ford 1502 Accepted 444K 0MS G++
  3. */
  4. #include"cstdio"
  5. #include"cstring"
  6. #include"algorithm"
  7. using namespace std;
  8. const int MAXN=;
  9. const int INF=0X3fffffff;
  10. struct Edge{
  11. int from,to,cost;
  12. }es[MAXN];
  13. int V,E;
  14. int ford(int s)
  15. {
  16. int d[MAXN];
  17. for(int i=;i<=V;i++) d[i]=INF;
  18. d[s]=;
  19.  
  20. while(true)
  21. {
  22. bool update=false;
  23. for(int i=;i<E;i++)
  24. {
  25. Edge e=es[i];
  26. if(d[e.from]!=INF&&d[e.to]>d[e.from]+e.cost)
  27. {
  28. d[e.to]=d[e.from]+e.cost;
  29. update=true;
  30. }
  31. }
  32. if(!update) break;
  33. }
  34. int ans=-;
  35. for(int i=;i<=V;i++)
  36. {
  37. if(d[i]<INF) ans=max(ans,d[i]);
  38. }
  39. return ans;
  40. }
  41. int main()
  42. {
  43. while(scanf("%d",&V)!=EOF)
  44. {
  45. E=;
  46. for(int i=;i<=V;i++)
  47. for(int j=;j<i;j++)
  48. {
  49. char x[];
  50. scanf("%s",x);
  51. if(x[]!='x')
  52. {
  53. es[E].from=i,es[E].to=j,es[E++].cost=atoi(x);
  54. es[E].from=j,es[E].to=i,es[E++].cost=atoi(x);
  55. }
  56. }
  57.  
  58. printf("%d\n",ford());
  59. }
  60.  
  61. return ;
  62. }
  1. /*
  2. poj1502 spfa Accepted 232K 16MS C++
  3. */
  4. #include<cstdio>
  5. #include<cstring>
  6. #include<queue>
  7. #include<algorithm>
  8. #include<vector>
  9. using namespace std;
  10. const int MAXN=;
  11. const int INF=0x3fffffff;
  12. struct Edge{
  13. int to,w;
  14. Edge(){}
  15. Edge(int cto,int cw):to(cto),w(cw){}
  16. };
  17. vector<Edge> mp[MAXN];
  18. int n;
  19. int d[MAXN],vis[MAXN];
  20. void spfa(int s)
  21. {
  22. memset(vis,,sizeof(vis));
  23. for(int i=;i<=n;i++)
  24. d[i]=INF;
  25. queue<int> que;
  26. d[s]=,vis[s]=;
  27. que.push(s);
  28. while(!que.empty())
  29. {
  30. int now=que.front();que.pop();
  31. vis[now]=;
  32. for(int i=;i<mp[now].size();i++)
  33. {
  34. Edge e=mp[now][i];
  35. if(d[e.to]>d[now]+e.w)
  36. {
  37. d[e.to]=d[now]+e.w;
  38. if(!vis[e.to])
  39. {
  40. vis[e.to]=;
  41. que.push(e.to);
  42. }
  43. }
  44. }
  45. }
  46. int res=;
  47. for(int i=;i<=n;i++)
  48. res=max(d[i],res);
  49. printf("%d\n",res);
  50. }
  51. int main()
  52. {
  53. while(scanf("%d",&n)!=EOF)
  54. {
  55.  
  56. for(int i=;i<=n;i++)
  57. mp[i].clear();
  58. for(int i=;i<=n;i++)
  59. for(int j=;j<i;j++)
  60. {
  61. char x[]="\0";
  62. scanf("%s",x);
  63. if(x[]=='x') continue;
  64. else
  65. {
  66. int l=atoi(x);
  67. mp[i].push_back(Edge(j,l));
  68. mp[j].push_back(Edge(i,l));
  69. }
  70. }
  71. spfa();
  72. }
  73. }
  1. /*
  2. floyd 1502 Accepted 428K 0MS G++
  3. */
  4. #include"cstdio"
  5. #include"algorithm"
  6. using namespace std;
  7. const int MAXN=;
  8. const int INF=0X3fffffff;
  9. int mp[MAXN][MAXN];
  10. int V;
  11. int main()
  12. {
  13. while(scanf("%d",&V)!=EOF)
  14. {
  15. for(int i=;i<=V;i++)
  16. for(int j=;j<=i;j++)
  17. {
  18. if(i==j){
  19. mp[i][j]=;
  20. continue;
  21. }
  22. char x[];
  23. scanf("%s",x);
  24. if(x[]!='x') mp[i][j]=mp[j][i]=atoi(x);
  25. else mp[i][j]=mp[j][i]=INF;
  26. }
  27.  
  28. for(int k=;k<=V;k++)
  29. for(int i=;i<=V;i++)
  30. for(int j=;j<=V;j++)
  31. mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
  32. int ans=-;
  33. for(int i=;i<=V;i++)
  34. if(mp[][i]<INF)
  35. ans=max(mp[][i],ans);
  36.  
  37. printf("%d\n",ans);
  38. }
  39.  
  40. return ;
  41. }

POJ1502(最短路入门题)的更多相关文章

  1. hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  2. poj 2524:Ubiquitous Religions(并查集,入门题)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23997   Accepted:  ...

  3. poj 3984:迷宫问题(广搜,入门题)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description ...

  4. hdu 1754:I Hate It(线段树,入门题,RMQ问题)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. poj 3254 状压dp入门题

    1.poj 3254  Corn Fields    状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...

  6. zstu.4194: 字符串匹配(kmp入门题&& 心得)

    4194: 字符串匹配 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 206  Solved: 78 Description 给你两个字符串A,B,请 ...

  7. hrbustoj 1073:病毒(并查集,入门题)

    病毒Time Limit: 1000 MS Memory Limit: 65536 KTotal Submit: 719(185 users) Total Accepted: 247(163 user ...

  8. hdu 1465:不容易系列之一(递推入门题)

    不容易系列之一 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  9. hrbustoj 1545:基础数据结构——顺序表(2)(数据结构,顺序表的实现及基本操作,入门题)

    基础数据结构——顺序表(2) Time Limit: 1000 MS    Memory Limit: 10240 K Total Submit: 355(143 users) Total Accep ...

随机推荐

  1. 30天自制操作系统(三)进入32位模式并导入C语言

    1 制作真正的IPL IPL(Initial Program Loader),启动程序装载器,但是之前并没有实质性的装载任何程序,这次作者要开始装载程序了. 虽然现在开发的操作系统啥功能也没有,作者说 ...

  2. oracle 推断字符是否为字母

    create or replace function ischar(chr varchar2) return varchar2 is   ischr varchar2(5); begin   sele ...

  3. 14 nginx 中配置 expires缓存提升网站负载

    一:nginx 中配置 expires缓存提升网站负载 对于网站的图片,尤其是新闻站, 图片一旦发布, 改动的可能是非常小的.我们希望 能否在用户访问一次后, 图片缓存在用户的浏览器端,且时间比较长的 ...

  4. linux 上操作常用的命苦与出错的地方

    帮助信息 ./configure -help|grep mysql 出错提示安装libxml2 tar -zxvf libxml2xxxx.tar cd libxml2xxx ./configure ...

  5. 消息队列Handler的用法

    下面是每隔一段时间就执行某个操作,直到关闭定时操作: final Handler handler = new Handler(); Runnable runnable = new Runnable() ...

  6. iOS开发 两个内存错误的一般处理方法

    本文转载至 http://blog.sina.com.cn/s/blog_a843a8850101dxlj.html 由于iOS5.0之前没有自动应用计数机制,也没有Java那样的垃圾回收功能.我们都 ...

  7. Objective-c中的delegate浅析

    delegate初探 在ios开发中,我们常常会用到类似例如以下的对话框: 因此,例如以下这段代码我们也就非常熟悉了: - (IBAction)showSheet:(id)sender { UIAct ...

  8. 你不得不知的几款常用的在线API管理工具

    在项目开发过程中,总会涉及到接口文档的设计编写,之前使用的都是ms office工具,不够漂亮也不直观,变更频繁的话维护成本也更高,及时性也是大问题.基于这个背景,下面介绍几个常用的API管理工具,方 ...

  9. xutils3基本使用

    根目录下新建一个类继承application,调用xUtils3初始化方法 public class AtguiguApplication extends Application { @Overrid ...

  10. python之virtualenv 与 virtualenvwrapper 详解

    在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题: 亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难. 此时,我们需要对于不同的工程使用 ...