A new Graph Game
题意:给你一张N个节点的无向图。然后给出M条边,给出第 I 条边到第J条边的距离。然后问你是否存在子环,假设存在,则输出最成环的最短距离和
解析:构图:选定源点及汇点,然后将源点至个点流量置为1,花费置为0.然后使用最小费用流,当返回值流量和,即flow < n 时。则输出NO。由于全部边成环最少边数为N。
其余和tour一样求法。处理一下某两点距离为最短距离就可以。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; const int maxn = 10000;
const int maxm = 100000;
const int INF = 0xfffffff; struct Edge{
int to, next, cap, flow, cost;
}edge[ maxm ]; int head[ maxn ], tol;
int pre[ maxn ], dis[ maxn ];
bool vis[ maxn ]; int N; void init( int n ){
N = n;
tol = 0;
memset( head, -1, sizeof( head ) );
} void addedge( int u, int v, int cap, int cost ){
edge[ tol ].to = v;
edge[ tol ].cap = cap;
edge[ tol ].cost = cost;
edge[ tol ].flow = 0;
edge[ tol ].next = head[ u ];
head[ u ] = tol++;
edge[ tol ].to = u;
edge[ tol ].cap = 0;
edge[ tol ].cost = -cost;
edge[ tol ].flow = 0;
edge[ tol ].next = head[ v ];
head[ v ] = tol++;
} bool spfa( int s, int t ){
queue< int > q;
for( int i = 0; i < N; ++i ){
dis[ i ] = INF;
vis[ i ] = false;
pre[ i ] = -1;
}
dis[ s ] = 0;
vis[ s ] = true;
q.push( s );
while( !q.empty( ) ){
int u = q.front();
q.pop();
vis[ u ] = false;
for( int i = head[ u ]; i != - 1; i = edge[ i ].next ){
int v = edge[ i ].to;
if( edge[ i ].cap > edge[ i ].flow && dis[ v ] > dis[ u ] + edge[ i ].cost ){
dis[ v ] = dis[ u ] + edge[ i ].cost;
pre[ v ] = i;
if( !vis[ v ] ){
vis[ v ] = true;
q.push( v );
}
}
}
}
if( pre[ t ] == -1 )
return false;
else
return true;
} struct node{
int f, c;
}; //node a;
node minCostMaxflow( int s, int t, int &cost ){
int flow = 0;
cost = 0;
while( spfa( s, t ) ){
int Min = INF;
for( int i = pre[ t ]; i != - 1; i = pre[ edge[ i ^ 1 ].to ] ){
if( Min > edge[ i ].cap - edge[ i ].flow )
Min = edge[ i ].cap - edge[ i ].flow;
}
for( int i = pre[ t ]; i != -1; i = pre[ edge[ i ^ 1 ].to ] ){
edge[ i ].flow += Min;
edge[ i ^ 1 ].flow -= Min;
cost += edge[ i ].cost * Min;
}
flow += Min;
}
node ans;
ans.f = flow;
ans.c = cost;
return ans;
} #define INF 0xfffffff
int mapp[ maxn ][ maxn ]; int main(){
int Case;
int n, m;
scanf( "%d", &Case );
for( int k = 1; k <= Case; ++k ){
scanf( "%d%d", &n, &m );
for( int i = 0; i <= n; ++i ){
for( int j = 0; j <= n; ++j ){
mapp[ i ][ j ] = INF;
}
}
int start = 0, end = 2 * n + 1, N = 2 * n + 2;
init( N );
int x, y, value;
for( int i = 1; i <= n; ++i ){
addedge( 0, i, 1, 0 );
addedge( n + i, end, 1, 0 );
}
int Max = 0;
for( int i = 0; i < m; ++i ){
scanf( "%d%d%d", &x, &y, &value );
int temp = max( x, y );
if( Max < temp ){
Max = temp;
}
if( mapp[ x ][ y ] > value ){
mapp[ x ][ y ] = mapp[ y ][ x ] = value;
}
} for( int i = 1; i <= n ; ++i ){
for( int j = 1; j <= n; ++j ){
if( mapp[ i ][ j ] != INF ){
addedge( i, n + j, 1, mapp[ i ][ j ] );
}
}
}
int cost;
node ans = minCostMaxflow( start, end, cost );
printf( "Case %d: ", k );
if( ans.f >= n ){
printf( "%d\n", ans.c );
}
else{
puts( "NO" );
}
}
return 0;
}
A new Graph Game的更多相关文章
- [开发笔记] Graph Databases on developing
TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...
- Introduction to graph theory 图论/脑网络基础
Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...
- POJ 2125 Destroying the Graph 二分图最小点权覆盖
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8198 Accepted: 2 ...
- [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] Graph Valid Tree 图验证树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] Clone Graph 无向图的复制
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- 讲座:Influence maximization on big social graph
Influence maximization on big social graph Fanju PPT链接: social influence booming of online social ne ...
- zabbix利用api批量添加item,并且批量配置添加graph
关于zabbix的API见,zabbixAPI 1item批量添加 我是根据我这边的具体情况来做的,本来想在模板里面添加item,但是看了看API不支持,只是支持在host里面添加,所以我先在一个ho ...
- Theano Graph Structure
Graph Structure Graph Definition theano's symbolic mathematical computation, which is composed of: A ...
- 纸上谈兵: 图 (graph)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 图(graph)是一种比较松散的数据结构.它有一些节点(vertice),在某些节 ...
随机推荐
- php-5.3源码编译autoconf版本不符合解法
1. 网上下载符合版本的autoconf 2. 卸载本地原本的autoconf 3. 解压autoconf 后进入目录 ./configure && make && s ...
- 02使用常规步骤编译NanoPiM1Plus的Android4.4.2
02使用常规步骤编译NanoPiM1Plus的Android4.4.2 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 21504965 AT qq.com 完成时间:2017/12/5 17:51 版本 ...
- 逻辑回归(Logistic Regression)推导
出自BYRans博客:http://www.cnblogs.com/BYRans/ 本文主要讲解分类问题中的逻辑回归.逻辑回归是一个二分类问题. 二分类问题 二分类问题是指预测的y值只有两个取值(0或 ...
- java生成excel
package test.poi; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; ...
- cstring 转string
(1)CString转换为string CString cs(_T("cs")); string s; s = (LPCSTR)(CStringA)(cs); (2)string转 ...
- CAD如何直接打印,不出现打印对话框?
主要用到函数说明: MxDrawXCustomFunction::Mx_Print 直接打印,不出现打印对话框,详细说明如下: 参数 说明 double ptLBx 打印的范围左下角x double ...
- 02C语言基本语法
C语言基本语法 注释 //行注释 /* 块注释*/ 标识符 标识符是用来标识变量.函数.任何其他用户自定义项目的名称:一个标识符以字母 A-Z 或 a-z 或下划线 _ 开始,后跟零个或多个字母.下划 ...
- HDU多校Round 7
Solved:2 rank:293 J. Sequense 不知道自己写的什么东西 以后整数分块直接用 n / (n / i)表示一个块内相同n / i的最大i #include <bits/s ...
- linux 系统 UDP 丢包问题分析思路
转自:http://cizixs.com/2018/01/13/linux-udp-packet-drop-debug?hmsr=toutiao.io&utm_medium=toutiao.i ...
- 我的ACM技能框架(自用)
每次接触到新的知识就把它名字记下来,留给以后当纪念 2018.11 已经学会的 滚动数组,前缀和优化 对多维数组在空间复杂度上的降维优化 最长上升子序列 LIS问题,动态规划递推解决 最长不下 ...