问题描述

As more and more transactions between companies and people are being carried out electronically over

the Internet, secure communications have become an important concern. The Internet Cryptographic

Protocol Company (ICPC) specializes in secure business-to-business transactions carried out over a

network. The system developed by ICPC is peculiar in the way it is deployed in the network.

A network like the Internet can be modeled as a directed graph: nodes represent machines or routers,

and edges correspond to direct connections, where data can be transmitted along the direction of an

edge. For two nodes to communicate, they have to transmit their data along directed paths from the

first node to the second, and from the second node to the first.

Figure: An arrow from node X to node Y means that it is possible to connect to node Y from node X

but not vice versa. If software is installed on nodes 1, 2, 7, and 8, then communication is possible

between node 1 and node 2. Other configurations are also possible but this is the minimum cost

option. This figure corresponds to the first sample input.

To perform a secure transaction, ICPC’s system requires the installation of their software not only

on the two endnodes that want to communicate, but also on all intermediate nodes on the two paths

connecting the end-nodes. Since ICPC charges customers according to how many copies of their software

have to be installed, it would be interesting to have a program that for any network and end-node pair

finds the cheapest way to connect the nodes.

输入格式

The input consists of several descriptions of networks. The first line of each description contains two

integers N and M (2 ≤ N ≤ 100), the number of nodes and edges in the network, respectively.

The nodes in the network are labeled 1, 2, . . . , N, where nodes 1 and 2 are the ones that want to

communicate. The first line of the description is followed by M lines containing two integers X and Y

(1 ≤ X, Y ≤ N), denoting that there is a directed edge from X to Y in the network.

The last description is followed by a line containing two zeroes.

输出格式

For each network description in the input, display its number in the sequence of descriptions. Then

display the minimum number of nodes on which the software has to be installed, such that there is

a directed path from node 1 to node 2 using only the nodes with the software, and also a path from

node 2 to node 1 with the same property. (Note that a node can be on both paths but a path need not

contain all the nodes.) The count should include nodes 1 and 2.

If node 1 and 2 cannot communicate, display ‘IMPOSSIBLE’ instead.

Follow the format in the sample given below, and display a blank line after each test case.

样例输入

8 12

1 3

3 4

4 2

2 5

5 6

6 1

1 7

7 1

8 7

7 8

8 2

2 8

2 1

1 2

0 0

样例输出

Network 1

Minimum number of nodes = 4

Network 2

IMPOSSIBLE

题目大意

n 个点m条边, 从点1走到点2再走回去, 求最少经过多少个不同的点. (n<=100)

解析

可以把经过1和2的环拆成从1出发到2的路径和从2到1的路径。如果我们建立原图的反图,那么就是求正图上从1出发到2的路径和反图上从1出发到2的路径最少经过的不同的点。这个可以利用动态规划加最短路的思想。设状态\(f[i][j]\)表示正图上到i、反图上到j经过的最少的不同点个数。转移时用Dijkstra最短路的形式,在正图和反图上分别沿着当前点的边扩展一次,如果更新就放进队列当中。有如下转移方程:

\[f[u1][v]=f[u1][u2]+[u1!=v],((u2,v)\in E2)\\
f[v][u2]=f[u1][u2]+[u2!=v],((u1,v)\in E1)
\]

但是,我们会发现,某些情况下\(f[i][j]\)的值会比实际情况多。对于这种有偏差的情况,我们可以换一种转移的方式。通过观察,发现这种情况下,\(f[i][j]\)是由\(f[j][i]\)多走j到i的最短路得到的。因此,我们有

\[f[u2][u1]=min(f[u2][u1],f[u1][u2]+dis[u1][u2]-1)
\]

\(dis[i][j]\)表示i到j的最短路,可用Floyd求出。注意,如果不是特殊情况,\(f[u1][u2]+dis[u1][u2]-1\)会得到错误的答案。所以要去最小值。

代码

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#define N 102
#define M 10002
using namespace std;
const int inf=0x3f3f3f3f;
struct node{
int x1,x2,d;
node(int _x1,int _x2,int _d){
x1=_x1;x2=_x2;d=_d;
}
};
bool operator < (node a,node b) { return a.d>b.d; }
int head1[N],ver1[M*2],nxt1[M*2],l1;
int head2[N],ver2[M*2],nxt2[M*2],l2;
int t,n,m,i,j,k,f[N][N],dis[N][N];
int read()
{
char c=getchar();
int w=0;
while(c<'0'||c>'9') c=getchar();
while(c<='9'&&c>='0'){
w=w*10+c-'0';
c=getchar();
}
return w;
}
void insert1(int x,int y)
{
l1++;
ver1[l1]=y;
nxt1[l1]=head1[x];
head1[x]=l1;
}
void insert2(int x,int y)
{
l2++;
ver2[l2]=y;
nxt2[l2]=head2[x];
head2[x]=l2;
}
void Dijkstra()
{
priority_queue<node> q;
memset(f,0x3f,sizeof(f));
f[1][1]=1;
q.push(node(1,1,1));
while(!q.empty()){
int x1=q.top().x1,x2=q.top().x2,d=q.top().d;
q.pop();
if(d!=f[x1][x2]) continue;
for(int i=head1[x1];i;i=nxt1[i]){
int y=ver1[i],w=(x2!=y);
if(d+w<f[y][x2]){
f[y][x2]=d+w;
q.push(node(y,x2,f[y][x2]));
}
}
for(int i=head2[x2];i;i=nxt2[i]){
int y=ver2[i],w=(x1!=y);
if(d+w<f[x1][y]){
f[x1][y]=d+w;
q.push(node(x1,y,f[x1][y]));
}
}
if(x1!=x2&&d+dis[x1][x2]-1<f[x2][x1]){
f[x2][x1]=d+dis[x1][x2]-1;
q.push(node(x2,x1,f[x2][x1]));
}
}
}
int main()
{
while(1){
n=read();m=read();
if(n==0&&m==0) break;
memset(head1,0,sizeof(head1));
memset(head2,0,sizeof(head2));
memset(dis,0x3f,sizeof(dis));
l1=l2=0;
for(i=1;i<=m;i++){
int u=read(),v=read();
insert1(u,v);
insert2(v,u);
dis[u][v]=1;
}
for(k=1;k<=n;k++){
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
if(dis[i][j]>dis[i][k]+dis[k][j]) dis[i][j]=dis[i][k]+dis[k][j];
}
}
}
printf("Network %d\n",++t);
if(dis[1][2]>=inf||dis[2][1]>=inf){
puts("Impossible");
puts("");
continue;
}
Dijkstra();
printf("Minimum number of nodes = %d\n",f[2][2]);
puts("");
}
return 0;
}

[UVa1057] Routing的更多相关文章

  1. 【UVA1057】Routing

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

  2. 【ZJOI2017 Round1练习&UVA1057】D6T1 Routing(DP,SPFA)

    题意:给你一个有向图, 并指定起点和终点. 问要从起点走向终点, 再从终点走向起点, 最少需要走过多少不同的节点. 对于 100%的数据, 有 N<=100, M<=min(1000,N* ...

  3. ASP.NET路由[ASP.NET Routing]

    ASP.NET路由[ASP.NET Routing] ASP.NET路由允许你在使用URL时不必匹配到网站中具体的文件,因为这个URL不必匹配到一个文件,你使用了描述用户行为且更容易被用户理解的URL ...

  4. 解读ASP.NET 5 & MVC6系列(12):基于Lamda表达式的强类型Routing实现

    前面的深入理解Routing章节,我们讲到了在MVC中,除了使用默认的ASP.NET 5的路由注册方式,还可以使用基于Attribute的特性(Route和HttpXXX系列方法)来定义.本章,我们将 ...

  5. 解读ASP.NET 5 & MVC6系列(11):Routing路由

    新版Routing功能介绍 在ASP.NET 5和MVC6中,Routing功能被全部重写了,虽然用法有些类似,但和之前的Routing原理完全不太一样了,该Routing框架不仅可以支持MVC和We ...

  6. [ASP.NET MVC 小牛之路]07 - URL Routing

    我们知道在ASP.NET Web Forms中,一个URL请求往往对应一个aspx页面,一个aspx页面就是一个物理文件,它包含对请求的处理. 而在ASP.NET MVC中,一个URL请求是由对应的一 ...

  7. ASP.NET MVC Routing学习笔记(一)

    Routing在ASP.NET MVC中是非常核心的技术,属于ASP.NET MVC几大核心技术之一,在使用Routing之前,得先引入System.Web.Routing,但其实不用这么麻烦,因为在 ...

  8. Routing 功能概述 - 每天5分钟玩转 OpenStack(98)

    路由服务(Routing)提供跨 subnet 互联互通功能. 例如前面我们搭建了实验环境: cirros-vm1      172.16.100.3        vlan100 cirros-vm ...

  9. .NET/ASP.NET Routing路由(深入解析路由系统架构原理)

    阅读目录: 1.开篇介绍 2.ASP.NET Routing 路由对象模型的位置 3.ASP.NET Routing 路由对象模型的入口 4.ASP.NET Routing 路由对象模型的内部结构 4 ...

随机推荐

  1. Linux_用户权限管理

    目录 目录 用户管理 useradd创建用户 userdel删除用户 usermod修改用户账号 passwd修改用户密码 用户权限设置 用户组管理 查看用户的属组 修改用户组gpasswd 为没有家 ...

  2. 是否被封禁ip或端口的检测网站 ping

    国内的: http://tool.chinaz.com/port (可以检测端口) https://tools.ipip.net/ping.php (貌似不可以检测端口) 国外的: https://w ...

  3. 2018.03.28 python-pandas groupby使用

    groupby 分组统计 1.根据某些条件将数据分组 2.对每个组独立应用函数 3.将结果合并到一个数据结构中 Dataframe在行或列上分组,将一个函数应用到各个分组并产生一个新值,然后函数执行结 ...

  4. Spring的应用上下文ApplicationContext

    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes() ...

  5. 解决 WordPress 后台加载非常缓慢/打不开问题

    在新版的 WordPress 中,为了后台的美观度,开发者在页面上加入了 Google Web 字体,这本来会让英文显示更加精美.我们只要移除 Google 在线字体即可恢复原来的速度.在你的主题的 ...

  6. 浅谈 JVM 结构体系、类加载、JDK JRE JVM 三者的关系

    一.java类,创建.编译.到运行的工程: 1.随便建一个Java类,保存后就是一个.java文件, 2.然后我们使用 javac命令编译 .java文件,生产 .class文件. 3.再然后使用 j ...

  7. echars 饼图 --》二次封装

    <template> <!-- 饼状图 1. 调用页面引入 import EcharsPie from '@/components/echarsPie.vue'; 注:自定义的组件名 ...

  8. angular5 给元素添加自定义属性

    今天尝试给一个a 标签添加一个自定义属性,用于存放相关数据,但是angular templates 编译不通过. <a href="javascript:void(0);" ...

  9. SparkStreaming DStream转换

    1.无状态转换操作 (1)无状态转化操作就是把简单的RDD转化操作应用到每个批次上,也就是转换DStream中的每一个RDD. 部分无状态转化操作: (2)尽管这些函数韩起来像作用在整个流上一样,但事 ...

  10. 【暑假培训1】test1

    T1: 30pts:直接暴力三层循环枚举 就就就先不写代码了qwq: 70pts: 因为X+Y+Z==0 所以我们可以考虑枚举X和Y,然后利用↑式子求出Z=-X-Y: 然后touli xcg的70pt ...