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;
}
时间: 2024-10-16 16:05:47

A new Graph Game的相关文章

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

724G - Xor-matic Number of the Graph(线性基)

724G - Xor-matic Number of the Graph 题意: 待补~~ 参考http://www.cnblogs.com/ljh2000-jump/p/6443189.html

[CodeChef - GERALD07 ] Chef and Graph Queries

Read problems statements in Mandarin Chineseand Russian. Problem Statement Chef has a undirected graph G. This graph consists of N vertices and M edges. Each vertex of the graph has an unique index from 1 to N, also each edge of the graph has an uniq

HDOJ 5409 CRB and Graph 无向图缩块

无向图缩块后,以n所在的块为根节点,dp找每块中的最大值. 对于每一个桥的答案为两块中的较小的最大值和较小的最大值加1 CRB and Graph Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 113    Accepted Submission(s): 41 Problem Description A connected, undi

【Lintcode】137.Clone Graph

题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. How we serialize an undirected graph: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each

133. Clone Graph

题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each n

图像分割之(二)Graph Cut(图割)

[email protected] http://blog.csdn.net/zouxy09 上一文对主要的分割方法做了一个概述.那下面我们对其中几个比较感兴趣的算法做个学习.下面主要是Graph Cut,下一个博文我们再学习下Grab Cut,两者都是基于图论的分割方法.另外OpenCV实现了Grab Cut,具体的源码解读见博文更新.接触时间有限,若有错误,还望各位前辈指正,谢谢. Graph cuts是一种十分有用和流行的能量优化算法,在计算机视觉领域普遍应用于前背景分割(Image se

Tensorflow学习笔记2:About Session, Graph, Operation and Tensor

简介 上一篇笔记:Tensorflow学习笔记1:Get Started 我们谈到Tensorflow是基于图(Graph)的计算系统.而图的节点则是由操作(Operation)来构成的,而图的各个节点之间则是由张量(Tensor)作为边来连接在一起的.所以Tensorflow的计算过程就是一个Tensor流图.Tensorflow的图则是必须在一个Session中来计算.这篇笔记来大致介绍一下Session.Graph.Operation和Tensor. Session Session提供了O

codeforces 715B:Complete The Graph

Description ZS the Coder has drawn an undirected graph of n vertices numbered from 0 to n - 1 and m edges between them. Each edge of the graph is weighted, each weight is a positive integer. The next day, ZS the Coder realized that some of the weight

分析函数调用关系图(call graph)的几种方法

分析函数调用关系图(call graph)的几种方法 绘制函数调用关系图对理解大型程序大有帮助.我想大家都有过一边读源码(并在头脑中维护一个调用栈),一边在纸上画函数调用关系,然后整理成图的经历.如果运气好一点,借助调试器的单步跟踪功能和call stack窗口,能节约一些脑力.不过如果要分析的是脚本语言的代码,那多半只好老老实实用第一种方法了.如果在读代码之前,手边就有一份调用图,岂不妙哉?下面举出我知道的几种免费的分析C/C++函数调用关系的工具. 函数调用关系图(call graph)是图