mcpc2017—Faulty Robot

这里写图片描述
题意大意:
如图,红色边为强制边,起点为①,机器人在程序控制下每一步都会走强制边(若没有强制边则停下),但是这个程序有bug会使机器人可能会出错而不一定走强制边(机器人只会出错一次),问你机器人可能停下的地点有几个?

思路:
把强制边权值设为0,非强制边权值为1,跑一遍bfs,找距离①小于等于1的即可(因为只会出错一次)

注意:
一定要跑完bfs之后再判断距离!

输入

The first line contains two integers n and m, designating the number of nodes and number of edges such that 1 ≤ n ≤ 103, 0 ≤ m ≤ 104. The next m lines will each have two integers a and b, 1 ≤ |a|, b ≤ n and |a| ≠ b. If a > 0, there is a directed edge between nodes a and b that is not forced. If a < 0, then there is a forced directed edge from −a to b. There will be at most 900 such forced moves. No two directed edges will be the same. No two starting nodes for forced moves will be the same.

输出

Display the number of nodes at which the robot might come to a rest.

#include<bits/stdc++.h>
using namespace std;
const int INF=1e8+7;
int S[1110];//记录此点有无强迫出边
int dis[1110];
int vi[1110],visit[1110];
struct edge{
    int e,next;int cost;
}ed[300010];
int n,m,ans=0;
int head[30010],cnt=0;
void add(int start,int ennd,int c){//头插法,单向
    ed[cnt].e=ennd;
    ed[cnt].next=head[start];
    ed[cnt].cost=c;
    head[start]=cnt;//记录下标
    cnt++;
}
int spfa(int s){
    memset(dis,INF,sizeof(dis));
    memset(vi,0,sizeof(vi));
    queue<int>q;
    q.push(s);vi[s]=1;dis[s]=0;
    while(!q.empty()){
        int u=q.front();q.pop();
        vi[u]=0;
        for(int i=head[u];i!=-1;i=ed[i].next){
            if(dis[ed[i].e]>dis[u]+ed[i].cost){
                dis[ed[i].e]=dis[u]+ed[i].cost;


                    if(vi[ed[i].e]==0){
                        q.push(ed[i].e);
                        vi[ed[i].e]=1;
                    }
            }
            /**/

        }
    }
}
int main()
{
    cin>>n>>m;
    for(int i=0;i<=n;i++){
        head[i]=-1;
    }
    memset(S,0,sizeof(S));
    int x,y;
    for(int i=0;i<m;i++){
        scanf("%d%d",&x,&y);
        if(x>0){
            add(x,y,1);
            //S[x]=0;
        }
        else{
            add(-1*x,y,0);
            S[-1*x]=1;
        }
    }
    /*if(n==1){
        cout<<"1\n";return 0;
    }*/
    spfa(1);
    for(int i=1;i<=n;i++){
        if(dis[i]<=1&&S[i]==0){
                //if(visit[i]==0)
                    ans++;//cout<<ed[i].e<<endl;

        }

    }
    cout<<ans<<endl;
    //return 0;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Seeyouer/article/details/80050553