题目链接:https://cn.vjudge.net/problem/HDU-4370

题意

给一个矩阵C(nn),要我们找到一个矩阵X(nn),满足以下条件:

X_{12}+X_{13}+...X_{1n}=1

X_{1n}+X_{2n}+...X_{n-1n}=1

for each i (1<i<n), satisfies ∑X_{ki} (1<=k<=n)=∑X_{ij} (1<=j<=n).

min ∑C ij*X ij

思路

如果把X当成一个邻接矩阵,可以发现本题就是要找一个图,满足以下条件:

  1. 节点1有一个出度(注意不要1->1,因为要最小化边权),节点n有一个入度(同理不要n->n)
  2. 其他节点出度等于入度
  3. 最小化边权

很容易发现最短路是一种可能的情况(每个节点仅有一个出度入度)

另外还有一种情况需要考虑,就是起点和终点可以不连通,意思就是节点1节点n各参与一个互不连通的环

这还是要考虑连通问题啊

又没考虑,可烦,考虑开一个最短路专题总结一下

提交过程

WA1 没考虑连通性
WA2 int换long long
WA3 脑抽加上了1->1和n->n情况
AC 加上判断

代码

#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=320;
const long long INF=1LL<<60;
typedef pair<long long, int> Node;
struct Cmp{
bool operator () (const Node &a, const Node &b){
return a.first>b.first;
}
};
int G[maxn+5][maxn+5]; long long Dij(int n){
long long dist[maxn+5], ans=0, circle1=INF, circle2=INF;
priority_queue<Node, vector<Node>, Cmp> que; for (int i=0;i<=n; i++) dist[i]=INF;
dist[1]=0;
que.push(Node(dist[1], 1));
while (que.size()){
Node x=que.top(); que.pop();
if (x.first!=dist[x.second]) continue; int &from=x.second;
for (int to=1; to<=n; to++) if (to!=from){
int &dis=G[from][to]; if (to==1) circle1=min(circle1, dist[from]+dis);
if (dist[to]<=dist[from]+(long long)dis) continue;
dist[to]=dist[from]+(long long)dis;
que.push(Node(dist[to], to));
}
}//return dist[n]; ans=dist[n];
for (int i=0;i<=n; i++) dist[i]=INF;
dist[n]=0;
que.push(Node(dist[n], n));
while (que.size()){
Node x=que.top(); que.pop();
if (x.first!=dist[x.second]) continue; int &from=x.second;
for (int to=1; to<=n; to++) if (to!=from){
int &dis=G[from][to]; if (to==n) circle2=min(circle2, dist[from]+dis);
if (dist[to]<=dist[from]+(long long)dis) continue;
dist[to]=dist[from]+(long long)dis;
que.push(Node(dist[to], to));
}
}return min(ans, circle1+circle2);
} int main(void){
int n; while (scanf("%d", &n)==1 && n){
for (int y=1; y<=n; y++)
for (int x=1; x<=n; x++) scanf("%d", &G[y][x]);
printf("%lld\n", Dij(n));
} return 0;
}
Time Memory Length Lang Submitted
1107ms 2012kB 1790 G++ 2018-06-02 11:28:23

HDU-4370 '0 or 1' 最短路 要考虑连通性的更多相关文章

  1. HDU - 4370 0 or 1 最短路

    HDU - 4370 参考:https://www.cnblogs.com/hollowstory/p/5670128.html 题意: 给定一个矩阵C, 构造一个A矩阵,满足条件: 1.X12+X1 ...

  2. HDU 4370 0 or 1 (最短路)

    [题目链接](http://acm.hdu.edu.cn/showproblem.ph Problem Description Given a n/n matrix Cij (1<=i,j< ...

  3. HDU 4370 0 or 1 (最短路+最小环)

    0 or 1 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/R Description Given a n*n matrix ...

  4. HDU - 4370 0 or 1

    0 or 1 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  5. 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. ...

  6. HDU 4370 0 or 1(转化为最短路)题解

    思路:虽然是最短路专题里的,但也很难想到是最短路,如果能通过这些关系想到图论可能会有些思路.我们把X数组看做邻接矩阵,那么三个条件就转化为了:1.1的出度为1:2.n的入度为1:3.2~n-1的出度等 ...

  7. HDU 4370 0 or 1(spfa+思维建图+计算最小环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4370 题目大意:有一个n*n的矩阵Cij(1<=i,j<=n),要找到矩阵Xij(i< ...

  8. 思维题(转换) HDU 4370 0 or 1

    题目传送门 题意:题目巨晦涩的传递出1点和n点的初度等于入度等于1, 其余点出度和入度相等 分析:求最小和可以转换成求最短路,这样符合条件,但是还有一种情况.1点形成一个环,n点也形成一个环,这样也是 ...

  9. (中等) 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 ...

  10. 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 ...

随机推荐

  1. Event-driven programming-main loop

    In computer programming, event-driven programming is a programming paradigm in which the flow of the ...

  2. jsp+jdbc实现用户登录

    1.1 创建数据库表 表名:user 字段: userid   保存用户的登录id name     用户名 password 密码 1.2 实现思路 a. 用户登录,则需要有个一个表单页,此页面可输 ...

  3. ansible 定义主机用户和密码

    定义主机组用户和密码 [webservers] ansible[01:04] ansible_ssh_user='root' ansible_ssh_pass='AAbb0101' [root@ftp ...

  4. 从YV12到NV12

    Media SDK的decoder,vpp,encoder对输入输出格式有着严格的限制,现在仅仅支持NV12.那么如何从其他格式转化为NV12是日常工作中经常遇到的事情.本篇文章以此为目的,讨论如何将 ...

  5. 异常值(outlier)

    简介 在数据挖掘的过程中,我们可能会经常遇到一些偏离于预测趋势之外的数据,通常我们称之为异常值. 通常将这样的一些数据的出现归为误差.有很多情况会出现误差,具体的情况需要就对待: 传感器故障 -> ...

  6. Python 绘图与可视化 matplotlib(上)

    参考链接:https://www.cnblogs.com/dudududu/p/9149762.html 更详细的:https://www.cnblogs.com/zhizhan/p/5615947. ...

  7. [BZOJ1975]HH去散步 图论+矩阵

    ###[BZOJ1975]HH去散步 图论+矩阵 题目大意 要求出在一个m条边,n个点的图中,相邻两次走的边不能相同,求在t时间时从起点A走到终点B的路径方案总数.将答案mod45989 输入格式: ...

  8. spring boot基础

    1.ANT下面典型的项目层次结构.(1) src存放文件.(2) class存放编译后的文件.(3) lib存放第三方JAR包.(4) dist存放打包,发布以后的代码. 2.Source Folde ...

  9. [Oracle] Merge语句

    Merge的语法例如以下: MERGE [hint] INTO [schema .] table [t_alias] USING [schema .] { table | view | subquer ...

  10. iOS给label加入下划线

    UILabel *myLabel = [[UILabelalloc] ,, , )]; NSMutableAttributedString *content = [[NSMutableAttribut ...