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),在某些节 ...
随机推荐
- how to do a mass update in Laravel5 ( 在Laravel 5里面怎么做大量数据更新 )
Today, I had spent 3 hours to fix one problem, The old program has a bug, originally, when a user pr ...
- vs2012 jsoncpp 链接错误
解决: 项目->属性->C/C++->代码生成->运行库->设置与使用的.lib的版本一致.
- ubuntu部署java环境
一.安装java sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracl ...
- 表格 —— 一个单元格插入多个tags
<st #st [columns]="columns" [data]="data" [bordered]='true'> <ng-templa ...
- Oracle中的COALESCE,NVL,NVL2,NULLIF函数
http://jingyan.baidu.com/article/fa4125acaf898e28ac7092b9.html
- Python&机器学习总结(一)
① numpy中np.c_和np.r_ np.r_是按列连接两个矩阵,就是把两矩阵上下相加,要求列数相等,类似于pandas中的concat(). np.c_是按行连接两个矩阵,就是把两矩阵左右相加, ...
- 浅谈AC自动机模板
什么是AC自动机? 百度百科 Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法. 要学会AC自动机,我们必须知道什么是Trie,也就是字典树.Tr ...
- PHP 锁机制
应用环境 解决高并发,库存为负数的情况 阻塞模式 如果其他进程已经加锁文件,当前进程会一直等其他进程解锁文件后继续执行 flock($fp, LOCK_EX) // 文件锁 非阻塞模式 如果其他进程已 ...
- jsonview插件的常见使用方法整理
Jsonview是目前最热门的一款开发者工具插件,确切的来说jQuery JSONView是一款非常实用的格式化和语法高亮JSON格式数据查看器jQuery插件.它是查看json数据的神器. 下载地址 ...
- 创建sum求多元素的和
a = [1, 2, 3] b = [4, 5, 6] def sum_super(* args): s = 0 for i in args: s += sum(i) return s # print ...