题意:给你一个有向图, 并指定起点和终点。

问要从起点走向终点, 再从终点走向起点, 最少需要走过多少不同的节点。

对于 100%的数据, 有 N<=100, M<=min(1000,N*N)。 图中可能有重边或者自环

思路:

 const oo=;
var head1,head2,vet1,vet2,next1,next2:array[..]of longint;
q:array[..]of record
x,y:longint;
end;
dis,f:array[..,..]of longint;
inq:array[..,..]of boolean;
n,m,i,j,k,cas,tot1,tot2,x,y,z:longint; function min(x,y:longint):longint;
begin
if x<y then exit(x);
exit(y);
end; procedure spfa;
var i,j,u1,u2,e1,e2,v1,v2,t,w,t1,w1,tmp:longint;
begin
for i:= to n do
for j:= to n do
begin
inq[i,j]:=false;
dis[i,j]:=oo;
end;
t:=; w:=; t1:=; w1:=;
q[].x:=; q[].y:=; inq[,]:=true; dis[,]:=;
while t<w do
begin
inc(t); inc(t1);
if t1=(n*n)<< then t1:=;
u1:=q[t1].x; u2:=q[t1].y; inq[u1,u2]:=false; e1:=head1[u1];
while e1<> do
begin
v1:=vet1[e1];
tmp:=dis[u1,u2];
if v1<>u2 then inc(tmp);
if tmp<dis[v1,u2] then
begin
dis[v1,u2]:=tmp;
if not inq[v1,u2] then
begin
inc(w); inc(w1);
if w1=(n*n)<< then w1:=;
q[w1].x:=v1; q[w1].y:=u2; inq[v1,u2]:=true;
end;
end;
e1:=next1[e1];
end; e2:=head2[u2];
while e2<> do
begin
v2:=vet2[e2];
tmp:=dis[u1,u2];
if v2<>u1 then inc(tmp);
if tmp<dis[u1,v2] then
begin
dis[u1,v2]:=tmp;
if not inq[u1,v2] then
begin
inc(w); inc(w1);
if w1=(n*n)<< then w1:=;
q[w1].x:=u1; q[w1].y:=v2; inq[u1,v2]:=true;
end;
end;
e2:=next2[e2];
end; if (u1<>u2)and(dis[u1,u2]+f[u1,u2]-<dis[u2,u1]) then
begin
dis[u2,u1]:=dis[u1,u2]+f[u1,u2]-;
if not inq[u2,u1] then
begin
inc(w); inc(w1);
if w1=(n*n)<< then w1:=;
q[w1].x:=u2; q[w1].y:=u1; inq[u2,u1]:=true;
end;
end;
end;
end; procedure add1(a,b:longint);
begin
inc(tot1);
next1[tot1]:=head1[a];
vet1[tot1]:=b;
head1[a]:=tot1;
end; procedure add2(a,b:longint);
begin
inc(tot2);
next2[tot2]:=head2[a];
vet2[tot2]:=b;
head2[a]:=tot2;
end; begin
assign(input,'uva1057.in'); reset(input);
assign(output,'uva1057.out'); rewrite(output);
while not eof do
begin
read(n,m);
if n= then break;
for i:= to n do
begin
head1[i]:=;
head2[i]:=;
end;
tot1:=; tot2:=;
inc(cas);
writeln('Network ',cas);
for i:= to n do
for j:= to n do
if i<>j then f[i,j]:=oo;
for i:= to m do
begin
read(x,y);
f[x,y]:=;
add1(x,y);
add2(y,x);
end;
for i:= to n do
for j:= to n do
for k:= to n do f[j,k]:=min(f[j,k],f[j,i]+f[i,k]);
if (f[,]=oo)or(f[,]=oo) then
begin
writeln('Impossible');
writeln;
continue;
end;
spfa;
writeln('Minimum number of nodes = ',dis[,]);
writeln;
end;
close(input);
close(output);
end.

【ZJOI2017 Round1练习&UVA1057】D6T1 Routing(DP,SPFA)的更多相关文章

  1. 【UVA1057】Routing

    [UVA1057]Routing 题面 洛谷 题解 有一个比较好想的dp就是\(f_{i,j}\)表示第一个点在\(i\),第二个点在\(j\)的最小点数,但是直接搞不好转移. 考虑建出反图,那么\( ...

  2. POJ 3182 The Grove [DP(spfa) 射线法]

    题意: 给一个地图,给定起点和一块连续图形,走一圈围住这个图形求最小步数 本来是要做课件上一道$CF$题,先做一个简化版 只要保证图形有一个点在走出的多边形内就可以了 $hzc:$动态化静态的思想,假 ...

  3. 值得一做》关于一道DP+SPFA的题 BZOJ1003 (BZOJ第一页计划) (normal-)

    这是一道数据范围和评测时间水的可怕的题,只是思路有点难想,BUT假如你的思路清晰,完全了解怎么该做,那就算你写一个反LLL和反SLE都能A,如此水的一道题,你不心动吗? 下面贴出题目 Descript ...

  4. BZOJ1003物流運輸 DP + SPFA

    @[DP, SPFA] Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要\(n\)天才能运完.货物运输过程中一般要转 停好几个码头.物流公司通常会设计一条固定的运 ...

  5. HDU 4085 Peach Blossom Spring 斯坦纳树 状态压缩DP+SPFA

    状态压缩dp+spfa解斯坦纳树 枚举子树的形态 dp[i][j] = min(dp[i][j], dp[i][k]+dp[i][l]) 当中k和l是对j的一个划分 依照边进行松弛 dp[i][j]  ...

  6. 【ZJOI2017 Round1练习&BZOJ4774】D3T2 road(斯坦纳树,状压DP)

    题意: 对于边带权的无向图 G = (V, E),请选择一些边, 使得1<=i<=d,i号节点和 n − i + 1 号节点可以通过选中的边连通, 最小化选中的所有边的权值和. d< ...

  7. 【ZJOI2017 Round1练习&BZOJ4767】D1T3 两双手(排列组合,DP)

    题意: 100%的数据:|Ax|,|Ay|,|Bx|,|By| <= 500, 0 <= n,Ex,Ey <= 500 思路:听说这是一道原题 只能往右或者下走一步且有禁止点的简化版 ...

  8. 【ZJOI2017 Round1练习】D4T2 trie(贪心,状压DP)

    题意:现在 Matej 手上有 N 个英文小写字母组成的单词, 他想知道,如果将这 N 个单词中的字母分别进行重新排列,形成的字母树的节点数最少是多少. n<=16,len[i]<=100 ...

  9. 【ZJOI2017 Round1练习&BZOJ5354】D7T3 room(DP)

    题意: 思路: 写了两种版本 考场版本 ..,..]of longint; t:..,..]of longint; n,m,i,j,k,oo,ans,d1:longint; function min( ...

随机推荐

  1. zojDakar Rally(01背包)

    01背包 加上每次更新解题数目最多 总用时最少 因为要保证用时最少,要先把时长由小到大排序. 没排序 WA了几小时..链接 #include <iostream> #include< ...

  2. [转]C#Linq中的Union All/Union/Intersect和Top/Bottom和Paging和SqlMethods,skip,take,takewhile,skipwhile,编译查询等

    本文转自:http://www.cnblogs.com/suizhikuo/p/3791799.html 我们继续讲解LINQ to SQL语句,这篇我们来讨论Union All/Union/Inte ...

  3. AJPFX详解jsp的九大内置对象和四大作用域

    定义:可以不加声明就在JSP页面脚本(Java程序片和Java表达式)中使用的成员变量 JSP共有以下9种基本内置组件(可与ASP的6种内部组件相对应): 1.request对象(作用域) 客户端的请 ...

  4. 把Scheme翻译成Java和C++的工具

    一.为什么要写这个工具? 公司内容有多个项目需要同一个功能,而这些项目中,有的是用Java的,有的是用C++的,同时由于某些现实条件限制,无法所有项目都调用统一的服务接口(如:可能运行在无网络的情况下 ...

  5. R in action读书笔记(17)第十二章 重抽样与自助法

    12.4 置换检验点评 除coin和lmPerm包外,R还提供了其他可做置换检验的包.perm包能实现coin包中的部分功能,因此可作为coin包所得结果的验证.corrperm包提供了有重复测量的相 ...

  6. 四则运算 来自 http://www.cnblogs.com/ys1101/p/4368103.html

    #include<stdio.h> #include<math.h> #include<windows.h> ; ; void add() { int a,b,c, ...

  7. Java Web数据库篇之MySQL特性

    MySQL ExplainEXPLAIN 命令的输出内容大致如下: mysql> explain select * from user_info where id = 2\G********** ...

  8. viewer && ImageFlow 图片滚动组件 图片点击放大 可以滚轮放大缩小 viewer

    ImageFlow https://finnrudolph.com/products/imageflow https://github.com/countzero/ImageFlow http://w ...

  9. rsync_ssh

    rsync -av -e "ssh" /data/wwwroot/a1 node2:/data/wwwroot/

  10. IIS+php服务器无法上传图片解决办法

    查找网上资料,发现php.ini下面有2个地方关于上传的配置: file_uploads = On  这里设置是否允许HTTP上传,默认应该为ON的 ;upload_tmp_dir=  这里设置上传文 ...