【ZJOI2017 Round1练习&UVA1057】D6T1 Routing(DP,SPFA)
题意:给你一个有向图, 并指定起点和终点。
问要从起点走向终点, 再从终点走向起点, 最少需要走过多少不同的节点。
对于 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)的更多相关文章
- 【UVA1057】Routing
[UVA1057]Routing 题面 洛谷 题解 有一个比较好想的dp就是\(f_{i,j}\)表示第一个点在\(i\),第二个点在\(j\)的最小点数,但是直接搞不好转移. 考虑建出反图,那么\( ...
- POJ 3182 The Grove [DP(spfa) 射线法]
题意: 给一个地图,给定起点和一块连续图形,走一圈围住这个图形求最小步数 本来是要做课件上一道$CF$题,先做一个简化版 只要保证图形有一个点在走出的多边形内就可以了 $hzc:$动态化静态的思想,假 ...
- 值得一做》关于一道DP+SPFA的题 BZOJ1003 (BZOJ第一页计划) (normal-)
这是一道数据范围和评测时间水的可怕的题,只是思路有点难想,BUT假如你的思路清晰,完全了解怎么该做,那就算你写一个反LLL和反SLE都能A,如此水的一道题,你不心动吗? 下面贴出题目 Descript ...
- BZOJ1003物流運輸 DP + SPFA
@[DP, SPFA] Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要\(n\)天才能运完.货物运输过程中一般要转 停好几个码头.物流公司通常会设计一条固定的运 ...
- 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] ...
- 【ZJOI2017 Round1练习&BZOJ4774】D3T2 road(斯坦纳树,状压DP)
题意: 对于边带权的无向图 G = (V, E),请选择一些边, 使得1<=i<=d,i号节点和 n − i + 1 号节点可以通过选中的边连通, 最小化选中的所有边的权值和. d< ...
- 【ZJOI2017 Round1练习&BZOJ4767】D1T3 两双手(排列组合,DP)
题意: 100%的数据:|Ax|,|Ay|,|Bx|,|By| <= 500, 0 <= n,Ex,Ey <= 500 思路:听说这是一道原题 只能往右或者下走一步且有禁止点的简化版 ...
- 【ZJOI2017 Round1练习】D4T2 trie(贪心,状压DP)
题意:现在 Matej 手上有 N 个英文小写字母组成的单词, 他想知道,如果将这 N 个单词中的字母分别进行重新排列,形成的字母树的节点数最少是多少. n<=16,len[i]<=100 ...
- 【ZJOI2017 Round1练习&BZOJ5354】D7T3 room(DP)
题意: 思路: 写了两种版本 考场版本 ..,..]of longint; t:..,..]of longint; n,m,i,j,k,oo,ans,d1:longint; function min( ...
随机推荐
- LN : leetcode 646 Maximum Length of Pair Chain
lc 646 Maximum Length of Pair Chain 646 Maximum Length of Pair Chain You are given n pairs of number ...
- php防止页面刷新代码
//代理IP直接退出 empty($_SERVER['HTTP_VIA']) or exit('Access Denied'); //防止快速刷新 session_start(); $seconds ...
- spark shuffle:分区原理及相关的疑问
一.分区原理 1.为什么要分区?(这个借用别人的一段话来阐述.) 为了减少网络传输,需要增加cpu计算负载.数据分区,在分布式集群里,网络通信的代价很大,减少网络传输可以极大提升性能.mapreduc ...
- H.264学习笔记2——帧内预测
帧内预测:根据经过反量化和反变换(没有进行去块效应)之后的同一条带内的块进行预测. A.4x4亮度块预测: 用到的像素和预测方向如图: a~f是4x4块中要预测的像素值,A~Q是临块中解码后的参考值. ...
- TP-LINK路由器桥接功能实现(WDS)
弄过好几次路由器的桥接了,但每次都忘记了,要重新找资料.在此记录一下,方便以后使用. 准备工作: 1.设置本地连接/无线网络连接(取决于用哪个配置路由器):IP-192.168.1.100 掩码-25 ...
- jQuery 收缩展开效果
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...
- C# 获取文件编码
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- day25-1 网络架构与互联网组成
目录 网络架构 单机架构 CS架构 数据放在服务端和客户端的利与弊 BS架构 互联网和互联网的组成 互联网的硬件组成 互联网的软件组成 网络架构 单机架构 应用领域: 单机游戏 CS架构 基于网络,应 ...
- java混淆工具Jocky和Proguard
java混淆工具有很多种,这里介绍Jocky和Proguard 一:Jocky是金蝶中间件技术领袖袁红岗先生的个人作品(旧有名称JOC).原本是方便Apusic 应用服务器的开发,现在开放出来,供大家 ...
- CSU 2018年12月月赛 A 2213: Physics Exam
Description 高中物理老师总认为给学生文本形式的问题比给纯计算形式的问题要求更高.毕竟,学生首先得阅读和理解问题. 因此,他们描述一个问题不像”U=10V,I=5A,P=?”,而是”有一个含 ...