Street Race_usaco 4.3_spfa+暴力+dfs

99 篇文章 0 订阅
66 篇文章 0 订阅

Description


给定一个图,求起点到终点间的必经点,再找一个点使图分成两个没有共点的子图
PROGRAM NAME: race3

INPUT FORMAT


The input file contains a well-formed course with at most 50 points and at most 100 arrows. There are N+2 lines in the file. The first N+1 lines contain the endpoints of the arrows that leave from the points 0 through N respectively. Each of these lines ends with the number -2. The last line contains only the number -1.

OUTPUT FORMAT


Your program should write two lines. The first line should contain the number of unavoidable points in the input course, followed by the labels of these points, in ascending order. The second line should contain the number of splitting points of the input course, followed by the labels of all these points, in ascending order.

Analysis


数据很小,第一问暴力一下去掉的点然后判断图是否连通。
第二问显然在第一问的范围内找一个点,那么把第一问的答案暴力一下看看是否符合条件就可以了
又一次被格式恶心到了

Code


/*
ID:wjp13241
PROG:race3
LANG:C++
*/
#include <stdio.h>
#include <string.h>
#include <queue>
#define N 51
#define E N*N/2+1
using namespace std;
struct edge{int y,next;}e[E];
int ls[E],mid[N],vis[N],dis[N],prt[N],maxE=0,tot;
bool find=false;
void add(int x,int y){e[++maxE]=(edge){y,ls[x]},ls[x]=maxE;}
int spfa(int st,int ed,int p)
{
    memset(vis,0,sizeof(vis));
    memset(dis,63,sizeof(dis));
    queue<int>q;
    q.push(st);
    dis[st]=0;
    vis[st]=1;
    while (!q.empty())
    {
        int now=q.front();q.pop();
        for (int i=ls[now];i;i=e[i].next)
            if (e[i].y!=p&&dis[now]+1<dis[e[i].y])
            {
                dis[e[i].y]=dis[now]+1;
                if (!vis[e[i].y])
                {
                    vis[e[i].y]=1;
                    q.push(e[i].y);
                }
            }
        vis[now]=0;
    }
    return dis[ed];
}
void dfs(int now,int p)
{
    if (vis[now])
        return;
    tot++;
    vis[now]=1;
    for (int i=ls[now];i;i=e[i].next)
        if (e[i].y!=p)
            dfs(e[i].y,p);
}
int main()
{
    freopen("race3.in","r",stdin);
    freopen("race3.out","w",stdout);
    int n=0;
    int t;
    while (scanf("%d",&t)&&t!=-1)
    {
        n++;
        if (t==-2)
            continue;
        t++;
        if (n!=t)
            add(n,t);
        while (scanf("%d",&t)&&t!=-2)
            if (n!=++t)
                add(n,t);
    }
    int ans=0;
    for (int i=2;i<n;i++)
        if (spfa(1,n,i)>=10000)
            prt[++ans]=i;
    if (ans)
        printf("%d ",ans);
    else
        printf("%d",ans);
    for (int i=1;i<ans;i++)
        printf("%d ",prt[i]-1);
    if (ans)
        printf("%d",prt[ans]-1);
    printf("\n");
    int cnt=0;
    for (int i=1;i<=ans;i++)
    {
        tot=0;
        memset(vis,0,sizeof(vis));
        dfs(1,prt[i]);
        memset(vis,0,sizeof(vis));
        dfs(prt[i],prt[i]);
        if (tot==n)
            mid[++cnt]=prt[i]-1;
    }
    if (cnt)
        printf("%d ",cnt);
    else
        printf("%d",cnt);
    for (int i=1;i<cnt;i++)
        printf("%d ",mid[i]);
    if (cnt)
        printf("%d",mid[cnt]);
    printf("\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值