点击打开链接

题意:给你一张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的更多相关文章

  1. [开发笔记] Graph Databases on developing

    TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...

  2. Introduction to graph theory 图论/脑网络基础

    Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...

  3. POJ 2125 Destroying the Graph 二分图最小点权覆盖

    Destroying The Graph Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8198   Accepted: 2 ...

  4. [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), ...

  5. [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), ...

  6. [LeetCode] Clone Graph 无向图的复制

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  7. 讲座:Influence maximization on big social graph

    Influence maximization on big social graph Fanju PPT链接: social influence booming of online social ne ...

  8. zabbix利用api批量添加item,并且批量配置添加graph

    关于zabbix的API见,zabbixAPI 1item批量添加 我是根据我这边的具体情况来做的,本来想在模板里面添加item,但是看了看API不支持,只是支持在host里面添加,所以我先在一个ho ...

  9. Theano Graph Structure

    Graph Structure Graph Definition theano's symbolic mathematical computation, which is composed of: A ...

  10. 纸上谈兵: 图 (graph)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 图(graph)是一种比较松散的数据结构.它有一些节点(vertice),在某些节 ...

随机推荐

  1. 连接服务器的mysql

    在服务器配置好Mysql 数据库,在客户端连接,报错: 解决方法: 1.在MySQL 数据库中修改user表,将host 中的localhoust 改为 %: 2.配置访问数据库的全选 根据需要配置权 ...

  2. 最容易理解的HMM文章

    wiki上一个比较好的HMM例子 分类 隐马尔科夫模型 HMM(隐马尔科夫模型)是自然语言处理中的一个基本模型,用途比较广泛,如汉语分词.词性标注及语音识别等,在NLP中占有很重要的地位.网上关于HM ...

  3. JAVA 学习笔记 - 基础语法1

    1. 类的定义 public class  与  class的区别 一个java文件只有一个public class, 而且类名必须与文件名一致. 一个java文件可以有多个class定义,javac ...

  4. 三维重建:GitHub百度Apollo 2.0

    GitHub:https://github.com/ApolloAuto/apollo 1. 关于Apollo的数据:Apollo的数据会如何开放? 自动驾驶数据将包括具有高分辨率图像和像素级别标注的 ...

  5. 最优化方法系列:Adam+SGD—>AMSGrad

    自动调参的Adam方法已经非常给力了,不过这主要流行于工程界,在大多数科学实验室中,模型调参依然使用了传统的SGD方法,在SGD基础上增加各类学习率的主动控制,以达到对复杂模型的精细调参,以达到刷出最 ...

  6. CAD使用SetxDataDouble写数据(网页版)

    主要用到函数说明: MxDrawEntity::SetxDataDouble 写一个Double扩展数据,详细说明如下: 参数 说明 [in] BSTR val 字符串值 szAppName 扩展数据 ...

  7. JavaScript 中实现 sleep

    来自推特上 Windows 故障分析的笑话 图片来源:me.me 推上看到的笑话,Windows 故障分析的实现. 然后想起来 JavaScript 中如何实现这个 sleep() 函数让代码暂停指定 ...

  8. P4817 [USACO15DEC]Fruit Feast 水果盛宴

    P4817 [USACO15DEC]Fruit Feast 水果盛宴 现在Bessie的饱食度为 00 ,她每吃一个橙子,饱食度就会增加 AA :每吃一个柠檬,饱食度就会增加 BB .Bessie还有 ...

  9. linux基础常用语句--新手

    查询:ls查看全部内容:ls -n删除:rm -rf 文件名创建目录: mkdir解压:rpm -验证是否安装:rpm -p 文件名rpm -ivh --nodeps 不需要前置条件的安装查询当前路径 ...

  10. C语言之自定义__DATE__与__TIME__

    /******************************************************************* * > File Name: 05-ymd.c * &g ...