入门经典-训练指南5.1 图论基础例题

UVA - 11624
Fire!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1010;
const int INF = 1000000000;
typedef pair<int,int>PII;

char mp[maxn][maxn];
int t_fire[maxn][maxn] ,vis[maxn][maxn] , R , C;

const int dx[4] = {0 , -1 , 0 , 1};
const int dy[4] = {1 , 0 , -1 , 0};

bool check_f(int x,int y){
    if(x<0 || x>=R || y<0 || y>=C) return false;
    if(vis[x][y] || mp[x][y]=='#') return false;
    return true;
}

void bfs_fire()
{
    memset(vis , 0 , sizeof(vis));
    memset(t_fire , 0x7f , sizeof(t_fire));
    queue<PII>Q;
    for(int i = 0;i<R ;i++){
        for(int j=0;j<C;j++){
            if(mp[i][j] == 'F'){
                Q.push(make_pair(i*C+j , 0));
                vis[i][j] = 1;
            }
        }
    }
    while(!Q.empty()){
        PII cur = Q.front() ; Q.pop();
        int x = cur.first / C ,y = cur.first % C ,t = cur.second;
        t_fire[x][y] = t;
        for(int d = 0; d<4 ;d++){
            int nx = x + dx[d] , ny = y + dy[d];
            if(check_f(nx,ny)) {
                Q.push(make_pair(nx*C + ny , t+1));
                vis[nx][ny] = 1;
            }
        }
    }
}

bool check_j(int x,int y,int t){
    if(x<0 || x>=R || y<0 || y>=C) return false;
    if(vis[x][y] || mp[x][y] == '#') return false;
    return t < t_fire[x][y];
}

int bfs_joe()
{
    memset(vis , 0 , sizeof(vis));
    queue<PII>Q;
    for(int i = 0;i<R ;i++){
        for(int j=0;j<C;j++){
            if(mp[i][j] == 'J'){
                Q.push(make_pair(i*C+j , 0));
                vis[i][j] = 1;
                goto step1;
            }
        }
    }
    step1 :;

    while(!Q.empty()){
        PII cur = Q.front() ; Q.pop();
        int x = cur.first / C ,y = cur.first % C ,t = cur.second;
        if(x==0 || x==R-1 || y == 0 || y==C-1) return t+1;

        for(int d = 0; d<4 ;d++){
            int nx = x + dx[d] , ny = y + dy[d];
            if(check_j(nx,ny,t+1)) {
                Q.push(make_pair(nx*C + ny , t+1));
                vis[nx][ny] = 1;
            }
        }
    }
    return -1;
}


int main()
{
    int T;
    scanf("%d"  , &T);
    while(T--){
        scanf("%d%d" , &R , &C);
        for(int i=0;i<R;i++){
            scanf("%s" , mp[i]);
        }
        bfs_fire();
        int ans = bfs_joe();
        if(ans < 0) printf("IMPOSSIBLE\n");
        else printf("%d\n" ,ans);
    }
    return 0;
}




UVA - 10047
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int dx[4] = {0 , -1 , 0 , 1};
const int dy[4] = {1 , 0 , -1 , 0};

struct STATE{
    int x,y,color,dir ,time;
    STATE(int x1,int y1,int c1,int d1,int t1) : x(x1) ,y(y1) ,color(c1) , dir(d1) ,time(t1) {};
};

char mp[30][30];
int R , C, vis[30][30][5][4];
void get_next(const STATE &a , STATE &b ,int kind){
    b = a , b.time ++;
    if(kind == 0){
        b.x += dx[a.dir] , b.y += dy[a.dir] , b.color ++ , b.color %= 5 ;
    }
    else if(kind == 1){
        b.dir = (b.dir + 3) % 4;
    }
    else b.dir = (b.dir + 1) % 4;
}
bool check(const STATE & s){
    if(s.x<0 || s.x >= R || s.y < 0 || s.y >= C || mp[s.x][s.y] == '#') return false;
    if(vis[s.x][s.y][s.color][s.dir]) return false;
    return true;
}

int BFS()
{
    memset(vis , 0 ,sizeof(vis));
    queue<STATE>Q;
    for(int i=0;i<R;i++)
    for(int j=0;j<C;j++)if(mp[i][j] == 'S'){
        Q.push(STATE(i,j,0,1,0)) , vis[i][j][0][1] = 1;
    }
    while(!Q.empty()){
        STATE cur = Q.front() ; Q.pop();
        if(mp[cur.x][cur.y] == 'T' && cur.color == 0) return cur.time;
        STATE next = cur;
        for(int kind = 0 ; kind < 3 ; kind ++){
            get_next(cur , next , kind);
            if(check(next)) {
                Q.push(next) ;
                vis[next.x][next.y][next.color][next.dir] = 1;
            }
        }
    }
    return -1;
}

int main()
{
    int cas = 1;
    while(scanf("%d%d" ,&R , &C)!=EOF  && R + C){
        for(int i=0;i<R;i++) scanf("%s" , mp[i]);
        int ans = BFS();
        if(cas != 1) printf("\n");
        printf("Case #%d\n" , cas++);
        if(ans < 0) printf("destination not reachable\n");
        else printf("minimum time = %d sec\n" ,ans);
    }
    return 0;
}




UVA - 10054
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxc = 55;

int cnt[maxc] , adj[maxc][maxc] ;
int vis[maxc];
void dfs(int u){
    vis[u] = 1;
    for(int i=0;i<=50;i++) {
        if(adj[u][i] && !vis[i]) dfs(i);
    }
}
bool conected(int s){
    memset(vis , 0, sizeof(vis));
    dfs(s);
    for(int i=1;i<=50;i++) if(cnt[i]){
        if(!vis[i]) return false;
    }
    return true;
}
void solve(int u){
    for(int i=1;i<=50;i++) if(adj[u][i]) {
        adj[u][i]-- , adj[i][u]--;
        solve(i);
        printf("%d %d\n" , i ,u);
    }
}
inline int nextint(){int x; scanf("%d" ,&x); return x;}
int main()
{
    //freopen("input.txt" , "r" ,stdin);
    int T  , cas = 1;
    T = nextint();
    while(T--){
        memset(cnt , 0 ,sizeof(cnt));
        memset(adj , 0 , sizeof(adj));
        int N;
        N = nextint();
        for(int i = 0; i<N ;i++){
            int x ,y;
            x=nextint(), y=nextint();
            cnt[x]++ , cnt[y]++;
            adj[x][y]++, adj[y][x]++;
        }

        bool ok = true;
        int s=1;
        while(cnt[s]==0) s++;

        for(int i=1 ; i<50 ;i++){
            if(cnt[i]&1) {ok = false ; break; }
        }
        if(!conected(s)) ok = false;

        printf("Case #%d\n" ,cas++);
        if(ok){
            solve(s);
        }
        else printf("some beads may be lost\n");
        if(T) printf("\n");
    }
    return 0;
}




UVALive - 4255
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 12;
int small[maxn][maxn], eq[maxn][maxn] , in[maxn];
int sum[maxn] , n;
inline int nextint(){int x; scanf("%d" , &x); return x; }
inline char nextchar() {char c=getchar();while(c==' ' || c=='\n') c = getchar(); return c;}

void topu(){
    queue<int>Q;
    int inq[maxn]={0};
    for(int i=0;i<=n;i++) {
        if(!in[i]) Q.push(i), sum[i]=0 , inq[i] = 1;
    }
    while(!Q.empty()){
        int u = Q.front(); Q.pop();
        inq[u] = 0;
        for(int i=0;i<=n;i++) if(eq[u][i] && sum[u]>sum[i]){
            sum[i] = sum[u];
            if(!inq[i]) Q.push(i) , inq[i] = 1;
        }
        for(int i=0;i<=n;i++) if(small[u][i]) {
            sum[i] = sum[u]+1;
            if(!inq[i]) Q.push(i) , inq[i] = 1;
        }
    }
}

int main()
{
    int T=nextint();
    while(T--){
        memset(small, 0 ,sizeof(small));
        memset(eq , 0 , sizeof(eq));
        memset(in , 0, sizeof(in));
        memset(sum , 0, sizeof(sum));

        n=nextint();
        for(int i=1;i<=n;i++)
        for(int j=i;j<=n;j++){
            char c = nextchar();
            if(c=='+') small[i-1][j] = 1 , in[j]++;
            else if(c=='-') small[j][i-1] = 1 ,in[i-1]++;
            else eq[i-1][j] = eq[j][i-1] = 1;
        }
        topu();
        for(int i=1;i<=n;i++) printf("%d " , sum[i] - sum[i-1]);
        printf("\n");
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值