Codeforces Round #349 (Div. 2) D. World Tour 【spfa+暴力枚举】

/* ***********************************************
Author        :Maltub
Email         :[email protected]
Blog          :htttp://www.xiang578.com
************************************************ */

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
//#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define pb push_back
using namespace std;
typedef vector<int> VI;
typedef long long ll;
const ll mod=1000000007;
const int inf=0x3fffffff;
const int N=3000+10;
vector<int>g[N],gt[N];
int n,m,dis[N][N];
struct node
{
    int v,w;
}p[N][10],pt[N][10];

void spfa(int x)
{
    int inq[N],u,v;
    queue<int>q;
    memset(inq,0,sizeof(inq));
    for(int i=1;i<=n;i++)
        dis[x][i]=inf;
    dis[x][x]=0;
    q.push(x);
    inq[x]=1;
    while(!q.empty())
    {
        u=q.front();
        inq[u]=0;
        q.pop();
        for(int i=0;i<g[u].size();i++)
        {
            v=g[u][i];
            if(dis[x][v]>dis[x][u]+1)
            {
                dis[x][v]=dis[x][u]+1;
                if(inq[v]==0)
                {
                    inq[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
int main()
{
    int u,v,a,b,c,d,mx,tmp,a1,b1,c1,d1,k;
    scanf("%d%d",&n,&m);
    for(int i=0;i<=n;i++)
    {
        g[i].clear();
        gt[i].clear();
    }
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        gt[v].push_back(u);
    }
    for(int i=1;i<=n;i++)
    {
       for(int j=0;j<4;j++)
            pt[i][j].w=-1;
        spfa(i);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<4;j++)
            p[i][j].w=-1;
        for(int j=1;j<=n;j++)
        {
            v=j;
            if(dis[i][j]==inf||i==j) continue;
            p[i][4].w=dis[i][v];
            p[i][4].v=v;
            for(k=3;k>=0;k--)
            {
                if(p[i][k+1].w>p[i][k].w)
                    swap(p[i][k+1],p[i][k]);
                else
                    break;
            }
        }
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(dis[j][i]==inf||i==j) continue;
            pt[i][4].w=dis[j][i];
            pt[i][4].v=j;
            for(k=3;k>=0;k--)
            {
                if(pt[i][k+1].w>pt[i][k].w)
                    swap(pt[i][k+1],pt[i][k]);
                else
                    break;
            }
        }
    }

    mx=-1;
    for(a=1;a<=n;a++)
    {
        for(b=1;b<=n;b++)
        {
            tmp=dis[a][b];
            if(tmp==inf||a==b) continue;
            for(k=0;k<4;k++)
            {
                if(p[b][k].v==a||p[b][k].v==b) continue;
                else if(p[b][k].w==-1) continue;
                else break;
            }
            if(k==4) continue;
            tmp+=p[b][k].w;
            c=p[b][k].v;
            for(k=0;k<4;k++)
            {
                if(pt[a][k].v==a||pt[a][k].v==b||pt[a][k].v==c) continue;
                else if(pt[a][k].w==-1) continue;
                else break;
            }
            if(k<4)
            {
                if(mx<tmp+pt[a][k].w)
                {
                    a1=a;
                    b1=b;
                    c1=c;
                    d1=pt[a][k].v;
                    mx=tmp+pt[a][k].w;
                }
            }
        }
    }
    printf("%d %d %d %d\n",d1,a1,b1,c1);
    return 0;
}
时间: 2024-11-05 18:39:19

Codeforces Round #349 (Div. 2) D. World Tour 【spfa+暴力枚举】的相关文章

Codeforces Round #349 (Div. 2) D. World Tour 暴力最短路

D. World Tour A famous sculptor Cicasso goes to a world tour! Well, it is not actually a world-wide. But not everyone should have the opportunity to see works of sculptor, shouldn't he? Otherwise there will be no any exclusivity. So Cicasso will enti

Codeforces Round #417 (Div. 2) A. Sagheer and Crossroads 模拟 枚举

Codeforces Round #417 (Div. 2) A. Sagheer and Crossroads 模拟  枚举 题意 一个红绿灯 按逆时针方向一次给出各个路口的左转,直行,右转,以及行人车道让你判断,汽车是否有可能撞到行人 注意 当前车道的左转有可能撞到别的车道的行人的 题解 一大堆特判 1 #include <cstdio> 2 #include <cmath> 3 #include <cstdlib> 4 #include <cstring&g

Codeforces Round #349 (Div. 2)

终于又回到熟悉的Round了 数学 A - Pouring Rain 设个未知数,解方程,还好没有hack点 #include <bits/stdc++.h> typedef long long ll; const int N = 1e5 + 5; const double PI = acos (-1.0); int main() { double d, h, v, e; scanf ("%lf%lf%lf%lf", &d, &h, &v, &

Codeforces Round #279 (Div. 2) F. Treeland Tour(lis+dfs)

题目链接: huangjing 题意:告诉一个无向无环图,然后求在联通的路上的lis. 思路:枚举起点求lis 复杂度是n^2logn,貌似这复杂度对时间有点玄,估计是数据有点弱... 首先枚举起点,然后求lis,主要是用dfs求的,要用到回溯的思想,我觉得是只要更新了,就要保存那个操作,然后一旦这一次的搜索完成,那么就要立即回复g数组的值,因为有很多不同的路线,所以一条路走完后,就要回复以前的状态哦,免得影响另外一条路..我觉得尽管他们都说是暴力,但是我觉得这个题还是蛮好的... 题目: F.

Codeforces Round #349 (Div. 2) C. Reberland Linguistics DP+set

C. Reberland Linguistics First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in d

Codeforces Round #349 (Div. 2) C. Reberland Linguistics 【DP】

/* *********************************************** Author :Maltub Email :[email protected] Blog :htttp://www.xiang578.com ************************************************ */ #include <cstdio> #include <cstring> #include <iostream> #inclu

Codeforces Round #332 (Div. 2) D. Spongebob and Squares(枚举)

http://codeforces.com/problemset/problem/599/D 题意:给出一个数x,问你有多少个n*m的网格中有x个正方形,输出n和m的值. 思路: 易得公式为:$\sum_{i=0}^{n}(n-i)(m-i) $ 化简得:$\left [ n(n+1)-\frac{n(n+1)}{2}\right ]*m+\frac{n(n+1)(n+2)}{6}-\frac{n(n+1)}{2}*n$ 将n作为小的数,枚举n即可. 1 #include<iostream>

Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】

任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Let's call some square matrix with integer values in its cells palind

Codeforces Round #281 (Div. 2) A. Vasya and Football 暴力

A. Vasya and Football Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red