题目链接: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. zabbix部署监控端(server)以及页面优化

    实验环境准备 172.20.10.2 server.zabbix.com 172.20.10.3 agent.zabbix.com 172.20.10.8 windows10 Server 端 [ro ...

  2. PHP下的异步尝试二:初识协程

    PHP下的异步尝试系列 如果你还不太了解PHP下的生成器,你可以根据下面目录翻阅 PHP下的异步尝试一:初识生成器 PHP下的异步尝试二:初识协程 PHP下的异步尝试三:协程的PHP版thunkify ...

  3. .net 参数修饰符

    参数修饰符的作用 参数修饰符 作用 无 如果一个参数没有用参数修饰符标记,则认为它将按值传递(pass by value),这意味着被调用的方法收到原始数据的一份副本 out 输出参数由被调用的方法赋 ...

  4. php $_SERVER['PHP_SELF']安全漏洞

    REQUEST_URI 返回的是包括后面数据串的地址,如 index.php?str=1234 PHP_SELF 是 index.php ------------------------------- ...

  5. 什么叫openapi

    Open API即开放API,也称开放平台. 所谓的开放API(OpenAPI)是服务型网站常见的一种应用,网站的服务商将自己的网站服务封装成一系列API(Application Programmin ...

  6. POJ-1785-Binary Search Heap Construction(笛卡尔树)

    Description Read the statement of problem G for the definitions concerning trees. In the following w ...

  7. struts2请求过程源代码分析

    struts2请求过程源代码分析 Struts2是Struts社区和WebWork社区的共同成果.我们甚至能够说,Struts2是WebWork的升级版.他採用的正是WebWork的核心,所以.Str ...

  8. hive 运行sqlclient异常

    FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. java.lang.Runtim ...

  9. Http抓包工具--查尔斯

    查尔斯 查尔斯:http://www.charlesproxy.com/ 这是比較好用的抓包工具.有Mac.Windows.Linux版本号.能够相应用程序.浏览器.手机.手机模拟器进行抓包. 官方站 ...

  10. ES shard unassigned的解决方法汇总

    说下shard出现的几个状态说明: relocating_shards shows the number of shards that are currently moving from one no ...