uva oj 567 - Risk(Floyd算法)

简介:

/*
一张有20个顶点的图上。
依次输入每个点与哪些点直接相连。
并且多次询问两点间,最短需要经过几条路才能从一点到达另一点。

bfs 水过
*/
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<queue>
using namespace std;

struct node{
   int x, step;
   node(){
   
   }
   node(int x, int step){
      this->x=x;
      this->step=step;
   }
}; 


vector<int>v[25]; 
queue<node>q;
int vis[25];


int b, e;

void bfs(){
   while(!q.empty())  q.pop();
   node cur;
   q.push(node(b, 0));
   while(!q.empty()){
       cur=q.front();
       q.pop();
       if(cur.x==e){
           printf("%2d to %2d: %d\n", b, e, cur.step);
           return;
       }
       int len=v[cur.x].size();
       for(int i=0; i<len; ++i){
             if(v[cur.x][i]==e){
             printf("%2d to %2d: %d\n", b, e, cur.step+1);
             return;
          }
          if(!vis[v[cur.x][i]]){
               vis[v[cur.x][i]]=1;
             q.push(node(v[cur.x][i], cur.step+1));
          } 
       }
   }
}

int main(){
    int n, u;
    int cnt=0;
    while(scanf("%d", &n)!=EOF){
        while(n--){
           scanf("%d", &u);
           v[1].push_back(u);
           v[u].push_back(1);
        }
        for(int i=2; i<=19; ++i){
           scanf("%d", &n);
           while(n--){
              scanf("%d", &u);
              v[i].push_back(u);
              v[u].push_back(i);
           }
        }
        scanf("%d", &n);
        printf("Test Set #%d\n", ++cnt);
        while(n--){
           scanf("%d%d", &b, &e) ;
           bfs();
           memset(vis, 0, sizeof(vis));
        }
        printf("\n");
        for(int i=1; i<=20; ++i)
           v[i].clear();
           
    } 
    return 0;
}
/*
   Floyd 才是正解!
*/
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;

int map[25][25];

void Folyd(){
   for(int k=1; k<=20; ++k)
      for(int i=1; i<=20; ++i)
         for(int j=1; j<=20; ++j)
            if(map[i][j] > map[i][k] + map[k][j])
               map[i][j] = map[i][k] + map[k][j];
}


int main(){
    int n, u, b, e;
    int cnt=0;
    while(scanf("%d", &n)!=EOF){
        memset(map, 0x3f, sizeof(map));
        while(n--){
           scanf("%d", &u);
           map[1][u]=map[u][1]=1;
        }
        for(int i=2; i<=19; ++i){
           scanf("%d", &n);
           while(n--){
              scanf("%d", &u);
              map[u][i]=map[i][u]=1;
           }
        }
        scanf("%d", &n);
        printf("Test Set #%d\n", ++cnt);
        Folyd();
        while(n--){
           scanf("%d%d", &b, &e) ;
           printf("%2d to %2d: %d\n", b, e, map[b][e]);
        }
        printf("\n");
    }
}

目录
相关文章
|
8月前
UVa10484 - Divisibility of Factors(数论)
UVa10484 - Divisibility of Factors(数论)
41 1
|
8月前
UVa11420 - Chest of Drawers(动态规划)
UVa11420 - Chest of Drawers(动态规划)
30 0
|
8月前
UVa11710 - Expensive subway(最小生成树)
UVa11710 - Expensive subway(最小生成树)
27 0
|
8月前
UVa668 - Parliament(贪心)
UVa668 - Parliament(贪心)
29 0
|
数据挖掘
HDU-1032,The 3n + 1 problem(水题)
HDU-1032,The 3n + 1 problem(水题)
|
物联网 Go C++
洛谷【2】P1001 A+B Problem
洛谷【2】P1001 A+B Problem
|
算法 BI
约瑟夫问题(Josephus problem)的klog(n)解法
约瑟夫问题是一个经典的算法问题,其解决过程涉可能用到的数据结构有数组、链表,涉及的算法包括模拟、递归、递推、动态规划等等,因此非常适合做一道面试题。   问题描述: 首先n个候选人围成一个圈,依次编号为0..n-1。然后指定一个正整数k,并0号候选人开始按从1到k的顺序依次报数,n-1号候选人报数之后,又再次从0开始。当有人报到K时,这个人被淘汰,从圈里出去。下一个人从1开始重新报
4274 0