HDU-4370 '0 or 1' 最短路 要考虑连通性
题目链接: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,因为要最小化边权),节点n有一个入度(同理不要n->n)
- 其他节点出度等于入度
- 最小化边权
很容易发现最短路是一种可能的情况(每个节点仅有一个出度入度)
另外还有一种情况需要考虑,就是起点和终点可以不连通,意思就是节点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' 最短路 要考虑连通性的更多相关文章
- 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 (最短路)
[题目链接](http://acm.hdu.edu.cn/showproblem.ph Problem Description Given a n/n matrix Cij (1<=i,j< ...
- 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
0 or 1 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 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 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
题目传送门 题意:题目巨晦涩的传递出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 ...
随机推荐
- swift使用查阅资料备份3
自主学习之RxSwift(二) -----flatMap https://blog.csdn.net/chelongfei/article/details/50995603 RxSwift 系列(九) ...
- 工作需求——VBA操作打印机
因为最近做的事情比较多,平时也多用EXCEL,所以顺便学习EXCEL的功能性的东西 转载:https://msdn.microsoft.com/zh-tw/vba/excel-vba/articles ...
- js单体内置对象
js单体内置对象:js的内置对象,是ECMAScritp提供的.不依赖于宿主环境的对象,我的理解就是在我们开发之前js里面就已经存在的对象.单体内置对象就是是不需要通过new来实例化的,例如我们的st ...
- python3 列表操作
- 创建列表 #创建列表: list1 = [1, 2, 3, 4, 5] - 向列表中添加元素 - append # 向列表中添加元素: list1 = [1, 2, 3, 4, 5] list1. ...
- pytorch 3 activation 激活函数
2.3 Activation Function import torch import torch.nn.functional as F from torch.autograd import Vari ...
- ES6重点知识点总结(2)
ES6重点知识点总结(2) call和apply的作用是什么?区别是什么? call和apply的功能基本相同,都是实现继承或者转换对象指针的作用: 唯一不通的是前者参数是罗列出来的,后者是存到数组中 ...
- ArcGIS 安装
百度网盘下载链接 密码:tvm6 打开解压的文件后,第一步为安装licence manager(安装监听) 打开\ArcGIS10.4\LicenseManager中的Setup.exe 傻瓜式安装 ...
- ZOJ 3288 Domination
D - Domination Time Limit:8000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Descr ...
- Android自己定义百度地图缩放图标
自己定义实现Android百度地图的缩放图标,须要自己定义一个缩放控件,实现效果例如以下: 这里的缩放效果,实现了点击button能够对地图的放大缩小,通过手势放大与缩小也控制缩放图标的可用状态.详细 ...
- 【POJ 2485】 Highways
[POJ 2485] Highways 最小生成树模板 Prim #include using namespace std; int mp[501][501]; int dis[501]; bool ...