nyoj 1369-Checkpoints (spfa)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yjf3151731373/article/details/89712303

1369-Checkpoints

内存限制:64MB 时间限制:1000ms 特判: No

通过数:20 提交数:161 难度:4

题目描述:
As a landlocked country in central and southern Africa , the political situation has been relatively stable since the implementation of multi-party elections in ZBA in 1991. But the ZBA parliament passed the 90 day emergency order issued by the president on 11 days of local time . The tension is that the patriotic team led by the government troops and NPG leaders founded by aborigines started, in addition to the unlawful insurgents of illegal militants.

Chinese peacekeepers are going to the town of Kerver to seek Chinese foreign aid engineers.

The military map shows that there are many checkpoints in the war zone. It can be modeled as a directed graph: nodes represent checkpoints , and edges represents the roads. The goal is that the less peacekeepers pass the checkpoints, the safer it will be.

输入描述:
The first line of the input contains one integer T, which is the number of test cases (1<=T<=5). Each test case specifies:

 * Line 1:      N  M  A  B          (2 ≤ N ≤ 100)

N and M denote the number of nodes and edges in the directed graph respectively. The checkpoints are labeled 1, 2, …, N, where chinese peacekeepers at node A and foreign aid engineers at node B.

*Line 2~m+1: Xi Yi (i=1, …., M)

followed by M lines containing two integers Xi and Yi (1 ≤ Xi, Yi ≤ N), denoting that there is a directed edge from node Xi to node Yi in the network.
输出描述:
For each test case generate a single line: a single integer that the minimum number of checkpoints . If a checkpoint is passed on the way back and forth , it will be counted only once.
样例输入:
复制
1
6 10 1 5
1 2
2 1
2 3
3 5
5 4
4 2
1 6
6 5
5 3
3 2
样例输出:
2
提示:

来源:
河南省第十一届ACM大学生程序设计竞赛

题意:找一条从起点到终点的回路,求最小点数(相同点算一次)
解:正解应该是强连通,这里用暴力跑最短路过了,主要就是取起点到终点的最小的前100条路径,反向最小的100条路径,然后求并集,暴力出奇迹(有点靠运气)

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<set>
using namespace std;
const int N = 1e6+10;
const int M = 500000;
typedef long long LL;
const LL mod = 20180520;
const int maxt=1000000000;
struct node
{
    int x;
    bool vis[105];
};
queue<node>q;
int head[110], cnt;
struct Q
{
    int to, next;
} p[10010];
set<int>mp1[110],mp2[110];
void init()
{
    memset(head,-1,sizeof(head));
    for(int i=0;i<=100;i++)mp1[i].clear(),mp2[i].clear();
    cnt=0;
}
void add(int u,int v)
{
    p[cnt].to=v,p[cnt].next=head[u];
    head[u]=cnt++;
    return ;
}
void bfs(int s,int t,set<int>st[110])
{
    node tmp;
    tmp.x=s;
    memset(tmp.vis,0,sizeof(tmp.vis));
    tmp.vis[s]=1;
    while(!q.empty())q.pop();
    q.push(tmp);
    int num=0;
    while(!q.empty())
    {
        tmp=q.front();
        q.pop();
        if(num>=100)break;
        int u=tmp.x;
        for(int i=head[u]; i!=-1; i=p[i].next)
        {
            int v=p[i].to;
            if(tmp.vis[v])continue;
            node o=tmp;
            o.vis[v]=1,o.x=v;
            if(v!=t)q.push(o);
            else
            {
                for(int i=1;i<=100;i++)
                {
                    if(o.vis[i])st[num].insert(i);
                }
                num++;
            }
        }
    }
    return ;
}
int solve()
{
    int ans=10000;
    for(int i=0;i<=100;i++)
    {
        if(mp1[i].size()==0)break;
        for(int j=0;j<=100;j++)
        {
            if(mp2[j].size()==0)break;
            set<int>mp=mp1[i];
            set<int>::iterator it;
            for(it=mp2[j].begin();it!=mp2[j].end();it++)mp.insert(*it);
            ans=min(ans,(int)mp.size());
        }
    }
    return ans;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n, m, a, b;
        scanf("%d %d %d %d", &n, &m, &a, &b);
        init();
        for(int i=0; i<m; i++)
        {
            int x,y;
            scanf("%d %d", &x, &y);
            add(x,y);
        }
        bfs(a,b,mp1);
        bfs(b,a,mp2);
        printf("%d\n",solve()-2);
    }
    return 0;
}




展开阅读全文

没有更多推荐了,返回首页