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");
    }
}

目录
相关文章
|
4月前
lanqiao OJ 2194 出差 bellman—ford
lanqiao OJ 2194 出差 bellman—ford
23 0
|
存储 算法 C++
剑指offer(C++)-JZ69:跳台阶(算法-动态规划)
剑指offer(C++)-JZ69:跳台阶(算法-动态规划)
UVa11420 - Chest of Drawers(动态规划)
UVa11420 - Chest of Drawers(动态规划)
68 0
UVa11710 - Expensive subway(最小生成树)
UVa11710 - Expensive subway(最小生成树)
64 0
UVa668 - Parliament(贪心)
UVa668 - Parliament(贪心)
81 0
|
算法
零基础学法100天第2天——bellman-ford(边数限制最短路算法)(下)
零基础学算法100天第2天——bellman-ford(边数限制最短路算法)
145 0
零基础学法100天第2天——bellman-ford(边数限制最短路算法)(下)
|
人工智能
POJ 2209 The King(简单贪心)
The King Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7499   Accepted: 4060 Description Once upon a time in a co...
900 0

热门文章

最新文章