Hướng dẫn giải của Các đại lý


Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của happyboy99x

#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;

#define TR(v,i) for(__typeof((v).begin())i=(v).begin();i!=(v).end();++i)
const int N = 250, INF = 1e9;
int d[N][N][2], n, s, t;
vector<int> g[N];

void enter() {
    int m; cin >> n >> m;
    cin >> s >> t; --s; --t;
    for(int i = 0; i < m; ++i) {
        int u, v; cin >> u >> v; --u; --v;
        g[u].push_back(v);
    }
}

void solve() {
    memset(d, 0x3f, sizeof d);
    queue<pair<pair<int, int>, int> > q;
    q.push(make_pair(make_pair(s, t), 0));
    d[s][t][0] = 0;
    while(!q.empty()) {
        int u = q.front().first.first, v = q.front().first.second;
        int who = q.front().second; q.pop();
        if(who == 0)
            TR(g[u], it) {
                int w = *it;
                if(d[w][v][1 - who] >= INF) {
                    d[w][v][1 - who] = d[u][v][who] + 1;
                    q.push(make_pair(make_pair(w, v), 1 - who));
                }
            } else TR(g[v], it) {
                int w = *it;
                if(d[u][w][1 - who] >= INF) {
                    d[u][w][1 - who] = d[u][v][who] + 1;
                    q.push(make_pair(make_pair(u, w), 1 - who));
                }
            }
    }
    int res = INF;
    for(int i = 0; i < n; ++i) res = min(res, d[i][i][0]);
    cout << (res == INF ? -1 : res / 2) << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    enter();
    solve();
    return 0;
}

Code mẫu của ladpro98

#include <iostream>
#include <cstdio>
#include <algorithm>
const int MAXN = 255;
const int MAXM = MAXN*MAXN;

using namespace std;

struct e {
    int x, y, turn;
};

int adj[MAXM], Link[MAXM], head[MAXN], n, m, s, t;
bool chk[MAXN][MAXN][2];
int d[MAXN][MAXN][2];
e q[MAXN*MAXN*2], u;

int main()
{
    //freopen("QBAGENTS.in", "r", stdin);
    scanf("%d %d", &n, &m);
    scanf("%d %d", &s, &t);
    int i, x, y;
    for(i=1; i<=m; i++) {
        scanf("%d %d", &x, &y);
        adj[i] = y;
        Link[i] = head[x];
        head[x] = i;
    }
    int l = 1;
    int r = 1;
    int res = -2;
    int v;
    q[1].x = s; q[1].y = t; q[1].turn = 1;
    while (l<=r) {
        if (res>=0) break;
        u = q[l]; l++;
        if (u.turn == 1) {
            i = head[u.x];
            while (i) {
                v = adj[i];
                if (!chk[v][u.y][2]) {
                    chk[v][u.y][2] = true;
                    r++;
                    q[r].x = v;
                    q[r].y = u.y;
                    q[r].turn = 2;
                    d[v][u.y][2] = d[u.x][u.y][1] + 1;
                }
                i = Link[i];
            }
        }
        else {
            i = head[u.y];
            while (i) {
                v = adj[i];
                if (!chk[u.x][v][1]) {
                    chk[u.x][v][1] = true;
                    r++;
                    q[r].x = u.x;
                    q[r].y = v;
                    q[r].turn = 1;
                    d[u.x][v][1] = d[u.x][u.y][2] + 1;
                    if (u.x == v) {
                        res = d[u.x][v][1];
                        break;
                    }
                }
                i = Link[i];
            }
        }
    }
    printf("%d", res/2);
    return 0;
}

Code mẫu của RR

//Wishing myself a happy lunar new year with a lot of accept solutions
//Written by Nguyen Thanh Trung
{$R+,Q+}
uses math;
const
  FINP='';
  FOUT='';
  MAXN=251;
var
  n,s,t:longint;
  xet:array[1..MAXN,1..MAXN,0..1] of byte;
  qd,qu,qv,qt:array[1..MAXN*MAXN*2] of longint;
  a:array[1..MAXN,1..MAXN] of longint;
  deg:array[1..MAXN] of longint;
  f1,f2:text;
procedure inp;
var
  i,m,u,v:longint;
begin
  read(f1,n,m,s,t);
  for i:=1 to m do
    begin
      read(f1,u,v);
      inc(deg[u]); a[u,deg[u]]:=v;
    end;
end;
procedure solve;
var
  first,last,u,v,uu,vv,i,d,tt:longint;
begin
  first:=1; last:=1; qu[1]:=s; qv[1]:=t; qt[1]:=0; qd[1]:=0;
  xet[s,t,0]:=1;
  while first<=last do
    begin
      u:=qu[first]; v:=qv[first]; tt:=qt[first]; d:=qd[first]; inc(first);
      if (u=v) and (tt=0) then
        begin
          writeln(f2,d);
          exit;
        end;
      if tt=0 then
        begin
          for i:=1 to deg[v] do
            begin
              vv:=a[v,i];
              if xet[u,vv,1]=0 then
                begin
                  xet[u,vv,1]:=1;
                  inc(last); qu[last]:=u; qv[last]:=vv; qt[last]:=1; qd[last]:=d;
                end;
            end;
        end
      else {tt=1}
        begin
          for i:=1 to deg[u] do
            begin
              uu:=a[u,i];
              if xet[uu,v,0]=0 then
                begin
                  xet[uu,v,0]:=1;
                  inc(last); qu[last]:=uu; qv[last]:=v; qt[last]:=0; qd[last]:=d+1;
                end;
            end;
        end;
    end;
  writeln(f2,-1);
end;
begin
  assign(f1,FINP); reset(f1);
  assign(f2,FOUT); rewrite(f2);
  inp;
  solve;
  close(f1); close(f2);
end.

Code mẫu của hieult

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <algorithm>

#include <ctime>
#include <deque>
#include <bitset>
#include <cctype>
#include <utility>
#include <cassert>

using namespace std;

typedef long long ll;
typedef double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

typedef pair<int, int> II;

const double PI = acos(-1.0);
const double eps = 1e-9;

const int dr[] = {-1, 0, +1, 0};
const int dc[] = {0, +1, 0, -1};

const int inf = (int)1e9 + 5;
const ll linf = (ll)1e17 + 5;
const ll mod = (ll)1e9 + 7;

#define maxn 255
#define maxe 150005

int n, m;
int adj[maxe], next[maxe], last[maxn], E, f[maxn][maxn], flag[maxn][maxn];
II que[maxn * maxn];
int size;

void add(int u, int v){
    adj[E] = v; next[E] = last[u]; last[u] = E++;
}

int main(){
//    freopen("in.txt", "r", stdin);

    cin >> n >> m;
    int s, t;
    cin >> s >> t;
    int u, v, uu, vv;
    ms(last, -1);  E = 0;
    For(run, 1, m){
        scanf("%d %d", &u, &v);
        add(u, v);
    }

    ms(f, -1);
    f[s][t] = 0;
    que[size++] = mp(s, t);
    ms(flag, 0);
    Rep(i, size){
        u = que[i].fi; v = que[i].se;
        if(u == v){
            cout << f[u][v] << endl;
            return 0;
        }
        for(int eu = last[u]; eu != -1; eu = next[eu]){
            uu = adj[eu];
            if(flag[uu][v]) continue;
            flag[uu][v] = 1;
            for(int ev = last[v]; ev != -1; ev = next[ev]){
                vv = adj[ev];
                if(f[uu][vv] < 0){
                    f[uu][vv] = f[u][v] + 1;
                    que[size++] = mp(uu, vv);
                }
            }
        }
    }
    cout << -1;

    return 0;
}

Code mẫu của ll931110

program QBAGENTS;
const
  input  = '';
  output = '';
  maxn = 250;
  maxk = 2 * sqr(maxn);
  maxv = 1000000;
type
  pnode = ^tnode;
  tnode = record
    val: longint;
    link: pnode;
  end;

  info = record
    x,y,z: longint;
  end;
var
  a: array[1..maxn] of pnode;
  F: array[1..maxn,1..maxn,1..2] of longint;
  q: array[1..maxk] of info;
  front,rear: longint;
  n,m,s,t: longint;
  res: longint;

procedure add(u,v: longint);
var
  p: pnode;
begin
  new(p);
  p^.val := v;
  p^.link := a[u];
  a[u] := p;
end;

procedure init;
var
  fi: text;
  i,u,v: longint;
begin
  assign(fi, input);
    reset(fi);

  readln(fi, n, m);
  readln(fi, s, t);
  for i := 1 to m do
    begin
      readln(fi, u, v);
      add(u,v);
    end;

  close(fi);
end;

procedure BFS;
var
  i,j,k: longint;
  p: pnode;
  u: info;
begin
  if s = t then
    begin
      res := 0;
      exit;
    end;

  for i := 1 to n do
   for j := 1 to n do
     for k := 1 to 2 do F[i,j,k] := maxv;

  F[s,t,1] := 0;

  front := 1;  rear := 1;
  with q[1] do
    begin
      x := s;  y := t;  z := 1;
    end;

  res := maxv;

  repeat
    u := q[front];
    inc(front);

    if u.z = 1 then
      begin
        p := a[u.x];
        while p <> nil do
          begin
            k := p^.val;
            p := p^.link;
            if F[k,u.y,2] = maxv then
              begin
                F[k,u.y,2] := F[u.x,u.y,1];
                inc(rear);
                with q[rear] do
                  begin
                    x := k;  y := u.y;  z := 2;
                  end;
              end;
          end;
      end
    else
      begin
        p := a[u.y];
        while p <> nil do
          begin
            k := p^.val;
            p := p^.link;
            if F[u.x,k,1] = maxv then
              begin
                F[u.x,k,1] := F[u.x,u.y,2] + 1;
                if u.x = k then
                  begin
                    res := F[u.x,k,1];
                    exit;
                  end;

                inc(rear);
                with q[rear] do
                  begin
                    x := u.x;  y := k;  z := 1;
                  end;
              end;
          end;
      end;
  until front > rear;
end;

procedure printresult;
var
  fo: text;
begin
  assign(fo, output);
    rewrite(fo);
    if res = maxv then writeln(fo, -1) else writeln(fo, res);
  close(fo);
end;

begin
  init;
  BFS;
  printresult;
end.

Code mẫu của khuc_tuan

// {$APPTYPE CONSOLE}
 {$mode delphi}

uses math, sysutils;

type
    ArrInt = array[0..7] of integer;

var
    n, m, s, t : integer;
    x, y, z, k, i, j, u, v : integer;
    f : array[0..250,0..7] of integer;
    a, b, c : ArrInt;

begin
    read(n,m,s,t);
    dec(s);
    dec(t);
    for i:=1 to m do
    begin
        read(u,v);
        dec(u);
        dec(v);
        f[u][v shr 5] := f[u][v shr 5] or (1 shl (v and 31)); 
    end;
    a[s shr 5] := a[s shr 5] or (1 shl (s and 31));
    b[t shr 5] := b[t shr 5] or (1 shl (t and 31));
    for i:=1 to 16000 do
    begin

        fillchar( c, sizeof(c), 0);
        for x:=0 to 7 do
            if a[x] <> 0 then
            begin
                y := x shl 5;
                if y + 31 >= n then z := n - 1 - y
                else z := 31;

                for k:=0 to z do
                begin
                    if (a[x] and (1 shl k))<>0 then
                    begin
                        for j:=0 to 7 do
                            c[j] := c[j] or f[y][j];
                    end;
                    inc(y);
                end;
            end;
        a := c;

        fillchar( c, sizeof(c), 0);
        for x:=0 to 7 do
            if b[x] <> 0 then
            begin
                y := x shl 5;
                if y + 31 >= n then z := n - 1 - y
                else z := 31;

                for k:=0 to z do
                begin
                    if (b[x] and (1 shl k))<>0 then
                    begin
                        for j:=0 to 7 do
                            c[j] := c[j] or f[y][j];
                    end;
                    inc(y);
                end;
            end;
        b := c;

        for x:=0 to 7 do
            if ((a[x] and b[x]) <> 0) then
            begin
                y := x shl 5;
                if y + 31 >= n then z := n - 1 - y
                else z := 31;

                for k:=0 to z do
                begin
                    if ((a[x] and (1 shl k))<>0) and ((b[x] and (1 shl k))<>0) then
                    begin
                        writeln(i);
                        exit;
                    end;
                end;
            end;

    end;
    writeln(-1);
end.

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.