HDU - 4370 0 or 1
0 or 1
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2811 Accepted Submission(s): 914
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.
For sample, X12=X24=1,all other Xij is 0.
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).
1 2 4 10
2 0 1 1
2 2 0 5
6 3 1 2
/*
HDU 4370 0 or 1
转换思维的题啊,由一道让人不知如何下手的题,转换为了最短路
基本思路就是把矩阵看做一个图,图中有n个点,1号点出度为1,
n号点入度为1,其它点出度和入度相等,路径长度都是非负数, 等价于一条从1号节点到n号节点的路径,故Xij=1表示需要经
过边(i,j),代价为Cij。Xij=0表示不经过边(i,j)。注意到Cij非负
且题目要求总代价最小,因此最优答案的路径一定可以对应一条简单路径。 最终,我们直接读入边权的邻接矩阵,跑一次1到n的最短路即可,记最短路为path。 漏了如下的情况B:
从1出发,走一个环(至少经过1个点,即不能
是自环),回到1;从n出发,走一个环(同理),回到n。
也就是1和n点的出度和入度都为1,其它点的出度和入度为0. 由于边权非负,于是两个环对应着两个简单环。 因此我们可以从1出发,找一个最小花费环,记代价为c1,
再从n出发,找一个最小花费环,记代价为c2。
(只需在最短路算法更新权值时多加一条记录即可:if(i==S) cir=min(cir,dis[u]+g[u][i])) 故最终答案为min(path,c1+c2)
*/
/*
本程序用SPFA来完成最短路。
但是由于要计算从出发点出发的闭环的路径长度。
所以要在普通SPFA的基础上做点变化。 就是把dist[start]设为INF。同时一开始并不是让出发点入队,而是让
出发点能够到达的点入队。
*/
//以上来自kuangbin的blog #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include <queue>
#include <vector>
#include<bitset>
using namespace std;
typedef long long LL;
const int maxn = ;
const int mod = +;
typedef pair<int,int> pii;
#define X first
#define Y second
#define pb push_back
//#define mp make_pair
#define ms(a,b) memset(a,b,sizeof(a)) const int inf = 0x3f3f3f3f;
int cost[maxn][maxn];
int dist[maxn];
int que[maxn];
bool vis[maxn]; void spfa(int st,int n){
int fro = ,rear = ;
for(int i=;i<=n;i++){
if(i==st){
dist[i]=inf;
vis[i]=false;
}else if(cost[st][i]!=inf){
dist[i]=cost[st][i];
que[rear++]=i;
vis[i]=true;
}else{
dist[i]=inf;
vis[i]=false;
}
} while(fro!=rear){
int u = que[fro++];
for(int v=;v<=n;v++){
if(dist[v]>dist[u]+cost[u][v]){
dist[v]=dist[u]+cost[u][v];
if(!vis[v]){
vis[v]=true;
que[rear++]=v;
if(rear>=maxn) rear=;
}
}
}
vis[u]=false;
if(fro>=maxn) fro=;
}
} int main(){
int n;
while(~scanf("%d",&n)){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&cost[i][j]);
}
}
spfa(,n);
int ans=dist[n];
int loop1=dist[];
spfa(n,n);
int loop2=dist[n];
ans=min(ans,loop1+loop2);
cout<<ans<<endl;
}
}
HDU - 4370 0 or 1的更多相关文章
- HDU 4370 0 or 1 (最短路+最小环)
0 or 1 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/R Description Given a n*n matrix ...
- HDU - 4370 0 or 1 最短路
HDU - 4370 参考:https://www.cnblogs.com/hollowstory/p/5670128.html 题意: 给定一个矩阵C, 构造一个A矩阵,满足条件: 1.X12+X1 ...
- HDU 4370 0 or 1(spfa+思维建图+计算最小环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4370 题目大意:有一个n*n的矩阵Cij(1<=i,j<=n),要找到矩阵Xij(i< ...
- 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
题目传送门 题意:题目巨晦涩的传递出1点和n点的初度等于入度等于1, 其余点出度和入度相等 分析:求最小和可以转换成求最短路,这样符合条件,但是还有一种情况.1点形成一个环,n点也形成一个环,这样也是 ...
- (中等) HDU 4370 0 or 1,建模+Dijkstra。
Description Given a n*n matrix C ij (1<=i,j<=n),We want to find a n*n matrix X ij (1<=i,j&l ...
- HDU 4370 0 or 1 (01规划)【Dijkstra】||【spfa】
<题目链接> 题目大意: 一个n*n的01矩阵,满足以下条件 1.X12+X13+...X1n=12.X1n+X2n+...Xn-1n=13.for each i (1<i<n ...
- hdu 4370 0 or 1,最短路
题目描述 给定n * n矩阵C ij(1 <= i,j <= n),我们要找到0或1的n * n矩阵X ij(1 <= i,j <= n). 此外,X ij满足以下条件: 1. ...
- HDU 4370 0 or 1(转化为最短路)题解
思路:虽然是最短路专题里的,但也很难想到是最短路,如果能通过这些关系想到图论可能会有些思路.我们把X数组看做邻接矩阵,那么三个条件就转化为了:1.1的出度为1:2.n的入度为1:3.2~n-1的出度等 ...
随机推荐
- HDU 2075 A|B?
http://acm.hdu.edu.cn/showproblem.php?pid=2075 Problem Description 正整数A是否能被正整数B整除,不知道为什么xhd会研究这个问题,来 ...
- CentOS查看版本及架构信息
https://blog.csdn.net/shuaigexiaobo/article/details/78030008
- 修复PLSQL Developer 与 Office 2010的集成导出Excel 功能
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\.htm]"PerceivedType"="text&qu ...
- [转帖]关于hostnamectl 命令
作者:Linux运维 来源:CSDN 原文:https://blog.csdn.net/linuxnews/article/details/51112022 版权声明:本文为博主原创文章,转载请附上博 ...
- 服务器RAID设置以及简单理解
备注: 适用于测试环境,生产环境暂时未验证 1. RAID种类 最高性能的RAID0 完全拆分所有的IO 不进行校验 但是单盘损坏, 数据完全丢失 最高损耗的RAID1 损失一半的存储容量, 做镜像, ...
- python矩阵水平镜像
方法1: label = label.T[::-1].transpose() 方法2: label = label[:,::-1] 方法3: 使用 numpy.fliplr https://docs. ...
- appium学习记录1
xpath定位: 语法 driver.find_element_by_xpath("//android.widget.EditText[@index="登陆"/../pr ...
- BZOJ1036[ZJOI2008]树的统计——树链剖分+线段树
题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v ...
- Django实现websocket完成实时通讯,聊天室,在线客服等
一 什么是Websocket WebSocket是一种在单个TCP连接上进行全双工通信的协议 WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据.在WebS ...
- HNOI2017单旋
单旋 这道题做法贼多,LCT,splay,线段树什么的貌似都行. 像我这种渣渣只会线段树了(高级数据结构学了也不会用). 首先离线所有操作,因为不会有两个点值重复,所以直接离散. 一颗线段树来维护所有 ...