BFS,DFS

ZOJ2913

题意:一个地图上被划分为很多块区域,每块区域有自己的编号,给定若干条公交线路及其经过的区域,求图中某一个区域使它能用最小的半径覆盖公交线路所经过的所有区域,一个区域的跨度就等于一个单位半径,中心区域本身也算一个单位半径。
这题是经典的bfs,找出公交线路经过的所有区域(公交车站),逐一进行bfs,计算出内部区域到所有车站的最大距离,再进行全图扫描,找出其中的最小距离

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define N 10010
#define INF  0x7fffffff//(1<<31)-1
struct ss{
    int id,step;
}node[N];
int edge[N][10],len[N],cnt[N];
bool bus[N],vis[N];
void bfs(int s)
{
    int i,h,t;
    memset(vis,0,sizeof(vis));
    node[0].step=1;
    node[0].id=s;
    h=t=0;
    vis[node[0].id]=1;
    while(h<=t)
    {
        if(node[h].step>len[node[h].id]) len[node[h].id]=node[h].step;
        for(i=0;i<cnt[node[h].id];i++)
        {
            if(!vis[edge[node[h].id][i]])
            {
                vis[edge[node[h].id][i]]=1;
                t++;
                node[t].step=node[h].step+1;
                node[t].id=edge[node[h].id][i];
            }
        }
        h++;
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int nz,nr,i,j,d,c;
        int mi=INF;
        int number;
        scanf("%d%d",&nz,&nr);
        memset(cnt,0,sizeof(cnt));
        memset(bus,0,sizeof(bus));
        memset(len,0,sizeof(len));
        for(i=0;i<nz;i++)
        {
            scanf("%d%d",&d,&c);
            cnt[d]=c;//记录队的大小
            for(j=0;j<c;j++)scanf("%d",&edge[d][j]);//入队
        }
        for(i=0;i<nr;i++)
        {
            scanf("%d",&d);
            for(j=0;j<d;j++)
            {
                scanf("%d",&c);
                bus[c]=1;//公交车经过的路径标记为1
            }
        }
        for(i=1;i<N;i++)
        {
            if(bus[i])bfs(i);//每个公交车线路经过的区域均要bfs
        }
        for(i=1;i<N;i++)//扫描全图,求出最小半径及标号
        {
            if(len[i]&&len[i]<mi)
            {
                mi=len[i];
                number=i;
            }
        }
        printf("%d %d\n",mi,number);
    }
    return 0;
}


ZOJ2416简单的bfs 1y

枚举出下一层对应的数字就可以了

枚举的函数写的烦了点

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
#define INF  0x7fffffff//(1<<31)-1
struct ss
{
    int n;
    int step;
};
queue<ss>q;
int b;
ss a;
int vis[10000];
int add(int n,int i)//i=1,2,3,4
{
    if(i==1){
        if(n%10==9)return n/10*10+1;
        else return n+1;
    }
    if(i==2)
    {
        if(n/10%10==9)return n/100*100+n%10+10;
        else return n+10;
    }
    if(i==3)
    {
        if(n/100%10==9)return n/1000*1000+n%100+100;
        else return n+100;
    }
    if(i==4)
    {
        if(n/1000==9)return n/10000*10000+n%1000+1000;
        else return n+1000;
    }
}
int plu(int n,int i)
{
    if(i==1){
        if(n%10==1)return n/10*10+9;
        else return n-1;
    }
    if(i==2)
    {
        if(n/10%10==1)return n/100*100+n%10+90;
        else return n-10;
    }
    if(i==3)
    {
        if(n/100%10==1)return n/1000*1000+n%100+900;
        else return n-100;
    }
    if(i==4)
    {
        if(n/1000==1)return n/10000*10000+n%1000+9000;
        else return n-1000;
    }
}
int change(int n,int i)//i=1,2,3
{
    int a,b;
    if(i==1)
    {
        a=n%10;
        b=n/10%10;
        return n/100*100+a*10+b;
    }
    if(i==2)
    {
        a=n/10%10;
        b=n/100%10;
        return n/1000*1000+a*100+b*10+n%10;
    }
    if(i==3)
    {
        a=n/100%10;
        b=n/1000;
        return a*1000+b*100+n%100;
    }
}
void bfs()
{
    ss t;
    int i;
    while(!q.empty())q.pop();
    memset(vis,0,sizeof(vis));
    a.step=0;
    q.push(a);
    vis[a.n]=1;
    while(!q.empty())
    {
        a=q.front();
        q.pop();
        if(a.n==b){
            printf("%d\n",a.step);
            return;
        }
        for(i=1;i<=4;i++)
        {
            t.n=add(a.n,i);
            if(!vis[t.n])
            {
                vis[t.n]=1;
                t.step=a.step+1;
                q.push(t);
            }
        }
        for(i=1;i<=4;i++)
        {
            t.n=plu(a.n,i);
            if(!vis[t.n])
            {
                vis[t.n]=1;
                t.step=a.step+1;
                q.push(t);
            }
        }
        for(i=1;i<=3;i++)
        {
            t.n=change(a.n,i);
            if(!vis[t.n])
            {
                vis[t.n]=1;
                t.step=a.step+1;
                q.push(t);
            }
        }
    }
}
int main()
{
    int x,y;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&a.n,&b);
        bfs();
    }
    return 0;
}

1003. Emergency (25)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input
5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
Sample Output
2 4

求最短路径的条数和最短路径的最长team数

总觉得这求最短路径是用bfs做的,不料得用dfs,不解

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
#define mod 10000007
#define N 550
int ren[N],vis[N];
int len[N][N];
int n,m,st,en;
int milen,maren,cnt;
void dfs(int node,int slen,int sren)
{
   // printf("--- %d %d %d\n",node,slen,sren);
    if(slen>milen)return;
    if(node==en){
        if(slen==milen){
            cnt++;;
            maren=max(maren,sren);
        }
        if(slen<milen){
            milen=slen;
            cnt=1;
            maren=sren;
        }
        return;
    }
    for(int i=0;i<n;i++)
    {
        if(!vis[i]&&len[node][i]!=-1)
        {
            vis[i]=1;
            dfs(i,slen+len[node][i],sren+ren[i]);
            vis[i]=0;
        }
    }
}
int main()
{
    while(~scanf("%d%d%d%d",&n,&m,&st,&en))
    {
        memset(len,-1,sizeof(len));
        for(int i=0;i<n;i++)scanf("%d",&ren[i]);
        for(int i=0;i<m;i++)
        {
            int a,b,l;
            scanf("%d%d%d",&a,&b,&l);
            len[a][b]=l;
            len[b][a]=l;
        }
        memset(vis,0,sizeof(vis));
        vis[st]=1;
        milen=10000000;
        maren=0;
        cnt=0;
        dfs(st,0,ren[st]);
        printf("%d %d\n",milen,maren);
    }
    return 0;
}


ZOJ 1671 用bfs可以过,用dfswa,不知道为什么

用HP[8][8]数组来代表蚂蚁走到每个格子时候的HP值,由于格子可以重复走,所以当BFS搜索散开时,可能会出现交叉,这样用HP[i][j]=HP[p1][p2]-1来计算时,就会出现混乱。。。。
所以在下一步前,先比较(HP[p1][p2]-1)和当前HP[i][j]的大小,如果比当前大,就接着执行,否则直接跳过。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
using namespace std;
int mapp[10][10];
int hp[10][10];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int n,m;
struct ss
{
    int x,y,hp,step;
}a,t;
queue<ss>q;
int jud(int x,int y)
{
    if(x<0||y<0||x>=n||y>=m)return 0;
    if(mapp[x][y]==0)return 0;
    return 1;
}
void bfs()
{
    while(!q.empty())q.pop();
    q.push(a);
    while(!q.empty())
    {
        a=q.front();
        q.pop();
        if(mapp[a.x][a.y]==4)a.hp=6;
        if(mapp[a.x][a.y]==3)
        {
            printf("%d\n",a.step);
            return;
        }
        for(int i=0;i<4;i++)
        {
            t.x=a.x+dx[i];
            t.y=a.y+dy[i];
            t.step=a.step+1;
            if(jud(t.x,t.y)&&a.hp-1>hp[t.x][t.y]){
                    t.hp=hp[t.x][t.y]=a.hp-1;
                    q.push(t);
            }
        }
    }
    printf("-1\n");
}
int main()
{
    while(~scanf("%d%d",&m,&n)&&(n+m))
    {
        memset(hp,0,sizeof(hp));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%d",&mapp[i][j]);
                if(mapp[i][j]==2){
                    a.x=i;
                    a.y=j;
                    a.hp=6;
                    a.step=0;
                    hp[i][j]=6;
                }
            }
        }
        bfs();
    }
    return 0;
}
/*
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
#define N 10
#define INF 1<<30
int n,m,sx,sy,ex,ey,mitim;
int mapp[N][N],vhp[N][N];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int jud(int x,int y)
{
    if(x<0||y<0||x>=n||y>=m)return 0;
    if(mapp[x][y]==0)return 0;
    return 1;
}
void dfs(int x,int y,int hp,int tim)
{
   // printf("--- %d %d %d %d\n",x,y,hp,tim);
    if(hp<=0)return;
    if(mapp[x][y]==4)hp=6;

    if(hp<=vhp[x][y])return;
    vhp[x][y]=hp;

    if(x==ex&&y==ey){
        if(tim<mitim)mitim=tim;
        return;
    }
    for(int i=0;i<4;i++)
    {
        int a=x+dx[i];
        int b=y+dy[i];
        if(jud(a,b)){
            dfs(a,b,hp-1,tim+1);
        }
    }
}
int main()
{
    while(~scanf("%d%d",&m,&n)&&(n+m))
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++){
                    scanf("%d",&mapp[i][j]);
                    if(mapp[i][j]==2){sx=i; sy=j; }
                    if(mapp[i][j]==3){ex=i; ey=j; }
            }
        }
        mitim=INF;
        memset(vhp,0,sizeof(vhp));
        dfs(sx,sy,6,0);
        if(mitim==INF)printf("-1\n");
        else printf("%d\n",mitim);
    }
    return 0;
}
*/







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值