nyoj 1369-Checkpoints (spfa)

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




孪生素数是指两个素数之间的差值为2的素数对。通过筛选法可以找出给定素数范围内的所有孪生素数的组数。 在引用的代码中,使用了递归筛选法来解决孪生素数问题。该程序首先使用循环将素数的倍数标记为非素数,然后再遍历素数数组,找出相邻素数之间差值为2的素数对,并统计总数。 具体实现过程如下: 1. 定义一个数组a[N,用来标记数字是否为素数,其中N为素数范围的上限。 2. 初始化数组a,将0和1标记为非素数。 3. 输入要查询的孪生素数的个数n。 4. 循环n次,每次读入一个要查询的素数范围num。 5. 使用两层循环,外层循环从2遍历到num/2,内层循环从i的平方开始,将素数的倍数标记为非素数。 6. 再次循环遍历素数数组,找出相邻素数之间差值为2的素数对,并统计总数。 7. 输出总数。 至此,我们可以使用这个筛选法的程序来解决孪生素数问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python用递归筛选法求N以内的孪生质数(孪生素数)](https://blog.csdn.net/weixin_39734646/article/details/110990629)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [NYOJ-26 孪生素数问题](https://blog.csdn.net/memoryofyck/article/details/52059096)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值