(中等) 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<=n),which is 0 or 1.
Besides,X ij meets the following conditions:
1.X 12+X 13+...X 1n=1
2.X 1n+X 2n+...X n-1n=1
3.for each i (1<i<n), satisfies ∑X ki (1<=k<=n)=∑X ij (1<=j<=n).
For example, if n=4,we can get the following equality:
X 12+X 13+X 14=1
X 14+X 24+X 34=1
X 12+X 22+X 32+X 42=X 21+X 22+X 23+X 24
X 13+X 23+X 33+X 43=X 31+X 32+X 33+X 34
Now ,we want to know the minimum of ∑C ij*X ij(1<=i,j<=n) you can get.
神题,可以转化为最短路问题。这个题可以一点一点的分析,首先就是选了第一行的第k个之后,就要选一个第k行的,所以建边 1->k 边权为第一行第k个的值,然后求1->n的最短路就好。。。。。。
不过这里还有一种特殊情况,就是1和其他形成一个环,N和其他形成一个环。所以答案就是两个环的和,和最短路中小的那一个。。。
代码如下:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=;
const int INF=10e8; void Dijkstra(int cost[][MaxN],int lowcost[],int N,int start)
{
priority_queue <int> que;
int t; for(int i=;i<=N;++i)
lowcost[i]=INF; que.push(start); while(!que.empty())
{
t=que.top();
que.pop(); for(int i=;i<=N;++i)
if(i!=t)
if(lowcost[t]==INF || lowcost[i]>lowcost[t]+cost[t][i])
{
lowcost[i]=(lowcost[t]==INF ? : lowcost[t])+cost[t][i];
que.push(i);
}
}
} int map1[MaxN][MaxN];
int ans[MaxN]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int N;
int t1,t2; while(~scanf("%d",&N))
{
for(int i=;i<=N;++i)
for(int j=;j<=N;++j)
scanf("%d",&map1[i][j]); Dijkstra(map1,ans,N,);
t1=ans[N];
t2=ans[]; Dijkstra(map1,ans,N,N);
t2+=ans[N]; printf("%d\n",min(t1,t2));
} return ;
}
(中等) HDU 4370 0 or 1,建模+Dijkstra。的更多相关文章
- 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 最短路
HDU - 4370 参考:https://www.cnblogs.com/hollowstory/p/5670128.html 题意: 给定一个矩阵C, 构造一个A矩阵,满足条件: 1.X12+X1 ...
- 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(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,最短路
题目描述 给定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的出度等 ...
随机推荐
- 2.1 工具使用:xmind
概念 心智图,又称脑图.思维导图.灵感触发图.概念地图或思维地图,是一种图像式思维的工具与及一种利用图像式思考辅助工具来表达思维的工具. 详细的可以查看这里(维基百科)还有这里(百度百科) 用了思维导 ...
- Echarts自适应浏览器大小
var myChart = echarts.init(document.getElementById('sitesChar')); var option = { title : { text: 'No ...
- js操作select和option
1.动态创建select function createSelect(){ var mySelect = document.createElement_x("select"); m ...
- MySQL 常用基础命令
一.启动与关闭 1.1 Linux下启动mysql 的命令: a. rpm包安装:service mysqld start b. 源码包安装:/usr/local/mysql/bin/mysqld_s ...
- UITabBarItem's appearance
1.我们知道,用tabBarController创建出来默认的tabBar似这个样子滴... -----------------我是图片分割线----------------------------- ...
- dedecms 标签的基本用法
1.关键描述调用标签: <meta name="keywords" content="{:fieldname='keywords'/}"> < ...
- 转一篇分析C语言调用时栈的变化的好文
http://blog.csdn.net/zsy2020314/article/details/9429707
- Java NIO Related
A file's status is 3-valued: The file is verified to exist; The file is verified to not exist; The f ...
- druid-1.0.13 数据库配置文件密码加密
1.cmd 切换到druid目录 我的是C:\tool\apache-tomcat-7.0.67\webapps\projectA\WEB-INF\lib 2.运行命令 java -cp druid ...
- 在win7/8/10鼠标右键添加带管理员权限的“在此处打开命令窗口”
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Drive\shell\runas]@="@shell32.dll,-8506 ...