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),在某些节 ...
随机推荐
- 创建http对象
package test; import java.net.HttpURLConnection;import java.net.URL; import javax.servlet.http.HttpS ...
- 联想 Vibe Shot(Z90-7) 免recovery 获取ROOT权限 救砖 VIBEUI V3.1_1625
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...
- Hue - Error loading MySQLdb module: libmysqlclient.so.20: cannot open shared object file: No such file or
解决下面两点异常 >> 1. Hue页面 点击DB 查询时弹出: Error loading MySQLdb module: libmysqlclient.so.20: cannot op ...
- if语句,while语句,do whlie语句,循环语句
总结: 1.定义数组并赋值: var arr=[1,2,3,4]; 2.通过下标访问数组: var str=arr[0]; 3.自定义数组 var arr=new Array(); 4.数组的赋值 a ...
- 服务器 获取用户 真实ip
在有代理的情况下,因为要代替客户端去访问服务器,所以,当请求包经过反向代理后,在代理服务器这里这个IP数据包的IP包头做了修改,最终后端WEB服务器得到的数据包的头部源IP地址是代理服务器的IP地址. ...
- 一段简单的手写Java计算器代码
import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.lang.*; public class Calc ...
- 10JDBC、CURD、XML、XPath
10JDBC.CURD.XML.XPath-2018/07/20 1.JDBC JDBC:java database connectivity JDBC与数据库驱动的关系:接口与实现的关系. JDBC ...
- LCS(HDU_5495 循环节)
传送门:LCS 题意:给出两个序列an和bn,想在给出一个序列pn,问经过a[p1],,,,a[pn]和b[p1],,,b[pn]变换后序列a和序列b的最长公共子序列的长度是多少. 思路:对a[i]- ...
- [LNOI2014]LCA(树链剖分)
BZOJ传送门 Luogu传送门 题目:给你一棵树,给你n个询问,每个询问要求输出$\sum_{i=l}^{r}depth(LCA(i,z))$ 细看看其实没有想象的那么难 大体思路: 1.对于每个询 ...
- nginx代理标准配置
#nginx开启的进程数worker_processes 4; #4核CPU #定义全局错误日志定义类型,[debug|info|notice|warn|crit]error_log ...