HDU4370:0 or 1(最短路)
0 or 1
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4370
Description:
Given a n*n matrix Cij (1<=i,j<=n),We want to find a n*n matrix Xij (1<=i,j<=n),which is 0 or 1.
Besides,Xij meets the following conditions:
1.X12+X13+...X1n=1
2.X1n+X2n+...Xn-1n=1
3.for each i (1<i<n), satisfies ∑Xki (1<=k<=n)=∑Xij (1<=j<=n).
For example, if n=4,we can get the following equality:
X12+X13+X14=1
X14+X24+X34=1
X12+X22+X32+X42=X21+X22+X23+X24
X13+X23+X33+X43=X31+X32+X33+X34
Now ,we want to know the minimum of ∑Cij*Xij(1<=i,j<=n) you can get.
Input:
The input consists of multiple test cases (less than 35 case).
For each test case ,the first line contains one integer n (1<n<=300).
The next n lines, for each lines, each of which contains n integers, illustrating the matrix C, The j-th integer on i-th line is Cij(0<=Cij<=100000).
Output:
For each case, output the minimum of ∑Cij*Xij you can get.
Sample Input:
4 1 2 4 10 2 0 1 1 2 2 0 5 6 3 1 2
Sample Output:
3
Hint:
For sample, X12=X24=1,all other Xij is 0.
题意:
给出一个s矩阵,每个位置都有对应的权值,然后要求你构造一个X矩阵,满足一下条件:
1.X12+X13+...+X1n = 1;
2.X1n+X2n+...+Xn-1n = 1;
3.当1<i<n时,∑Xki (1<=k<=n)=∑Xij (1<=j<=n)。
问在满足以上条件的情况下,∑Cij*Xij(1<=i,j<=n)的最小值为多少?
题解:
orz神题...十分巧妙。
主要的思路就是将我们构造的X矩阵当作邻接矩阵就行了,对应位置为1则代表对应的行和列之间有连一条边。
那么根据条件我们知道:1号点只有一个出度,n号点只有一个入度,且2~n-1号点入度出度相等。
想到这里了之后,还要想两种情况:第1种是从1号点出发直接到n号点,中间每个点的出入度相等;第2种是从1号点出发然后形成一个环最后回到1号点,对于n号点也同理。
对于第一种情况很好求解,直接跑次spfa就行了,注意下对应边的权值,就为S数组里面的权值。
对于第二种情况,从1和n出发跑两次spfa,然后在spfa里面加个判断就ok了。
两种的spfa可以合并一下,本题就解完了~
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = ;
int n,tot,mn;;
int vis[N],head[N],c[N][N],d[N];
struct Edge{
int v,w,next;
}e[N*N<<];
void adde(int u,int v,int w){
e[tot].v=v;e[tot].w=w;e[tot].next=head[u];head[u]=tot++;
}
void spfa(int s){
memset(vis,,sizeof(vis));
queue<int> q;q.push(s);vis[s]=;
memset(d,INF,sizeof(d));d[s]=;
while(!q.empty()){
int u=q.front();q.pop();vis[u]=;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]>d[u]+e[i].w){
d[v]=d[u]+e[i].w;
if(!vis[v]){
vis[v]=;
q.push(v);
}
}
if(v==s){
mn=min(mn,d[u]+e[i].w);
}
}
}
return ;
}
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&c[i][j]);
memset(head,-,sizeof(head));tot=;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(i==j) continue ;
adde(i,j,c[i][j]);
}
}
mn=INF;
spfa();
int tmp=mn,ans=INF;
mn=INF;
ans=d[n];
spfa(n);
ans=min(ans,mn+tmp);
cout<<ans<<endl;
}
return ;
}
HDU4370:0 or 1(最短路)的更多相关文章
- HDU-4370 '0 or 1' 最短路 要考虑连通性
题目链接:https://cn.vjudge.net/problem/HDU-4370 题意 给一个矩阵C(nn),要我们找到一个矩阵X(nn),满足以下条件: X_{12}+X_{13}+...X_ ...
- HDU4370 0 or 1 最短路
分析: 1001 (已更新) 显然,题目给的是一个0/1规划模型.解题的关键在于如何看出这个模型的本质.3个条件明显在刻画未知数之间的关系,从图论的角度思考问题,容易得到下面3个结论:1.X12+X ...
- 分层图 (可以选择K条路的权为0,求最短路)
分层图可以处理从图中选取k条边使其边权变为0,求最短路 Description 在你的强力援助下,PCY 成功完成了之前的所有任务,他觉得,现在正是出去浪的大好时光.于是,他来到高速公路上,找到一辆摩 ...
- hdu4370 0 or 1【最短路+建图】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297627.html ---by 墨染之樱花 题目链接:http://acm.hdu.ed ...
- HDU 4370 0 or 1 (最短路)
[题目链接](http://acm.hdu.edu.cn/showproblem.ph Problem Description Given a n/n matrix Cij (1<=i,j< ...
- HDU - 4370 0 or 1 最短路
HDU - 4370 参考:https://www.cnblogs.com/hollowstory/p/5670128.html 题意: 给定一个矩阵C, 构造一个A矩阵,满足条件: 1.X12+X1 ...
- hdu4370 比较抽象的最短路
题意: 给你一个n*n的矩阵,然后让咱们构造另一个n*n的矩阵,构造的矩阵有如下要求, 1.X12+X13+...X1n=1. 2.X1n+X2n+...Xn-1n=1. 3.for ea ...
- URAL 1002 Phone Numbers(KMP+最短路orDP)
In the present world you frequently meet a lot of call numbers and they are going to be longer and l ...
- poj3159 最短路(差分约束)
题意:现在需要分糖果,有n个人,现在有些人觉得某个人的糖果数不能比自己多多少个,然后问n最多能在让所有人都满意的情况下比1多多少个. 这道题其实就是差分约束题目,根据题中给出的 a 认为 b 不能比 ...
- short-path problem (Spfa) 分类: ACM TYPE 2014-09-02 00:30 103人阅读 评论(0) 收藏
#include <cstdio> #include <iostream> #include <cstring> #include <queue> #i ...
随机推荐
- python中一些内置函数实例
lambda表达式 简单函数可用lambda表达式 1. def f1() return(123) r1=f1() print() 2. f2=lambda:123 r2=f2() print() 以 ...
- 嵌入式Linux系统移植(二)——交叉编译工具集
常用工具:readelf.size.nm.strip.strings.objdump.objcopy.addr2line readelf:读可执行文件的elf头 ELF Header: Magic: ...
- P1396 营救(最小瓶颈路)
题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...
- abap<itab>refresh,clear,free
在ABAP开发过程中,clear,refresh,free都有用来清空内表的作用,但用法还是有区别的. clear itab,清空内表行以及工作区,但保存内存区. clear itab[],清空内表行 ...
- python--基本类型之列表
Lest(列表): 定义和创建列表: 列表:是python以及其他语言中最常用的数据结构之一.python用 [] 来解析列表列表是可变的.--可以改变列表的内容可以用切片 a=['张三','李四', ...
- 开启一个项目如何上传到git
1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库 git init 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点 ...
- Moodle的安装和登陆(使用Https)
之前使用默认的配置和安装,到中间检测组件是,总是提示 site no https.所以重新安装,用:https://localhost来登陆,结果不再提示,所以建议大家在使用时,还是用https来登 ...
- 如何在windows“我的电脑”中添加快捷文件夹
如图所示,windows中打开“我的电脑”时,原来有6个默认的文件夹,访问非常便捷,自己想再增加,可以使用“ThisPCTweaker”即可完成 操作如下图,不多解释,简单操作: 文件下载:http: ...
- Python 3基础教程23-多维列表
这里简单举例一个多维列表,多维看起来都很晕. # 多维列表 x = [ [5,6],[6,7],[7,2] ,[2,5] ,[4,9]] print(x) # 根据索引引用列表元素,例如打印[6,7] ...
- Cassandra 数据库设计
Cassandra 2.* CQL3.1 最近更新:2015-10-30 索引的设计 在Cassandra中经常会发现,索引不够用,不好用,各种不强大. 比如,我关注的人的需求uid + follow ...