poj 2949 Word Rings 参数搜索+负环探测

类似poj 3621。

代码:

//poj 2949
//sep9
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <cmath>
using namespace std;
const int maxL=1024;
const int maxM=100100;
char s[maxL];

struct Words
{
	int in,out,len;
}word[maxM];

map< string,int > mymap;
vector<int> ins[maxM],outs[maxM],g[maxM];
double mid,dis[maxM];
int n,cur,vis[maxM];

bool dfs(int u)
{
	vis[u]=cur;
	for(int i=g[u].size()-1;i>=0;--i){
		int v=g[u][i];
		if(dis[u]+mid-word[u].len*1.0<dis[v]){
			dis[v]=dis[u]+mid-word[u].len*1.0;
			if(vis[v]==cur||dfs(v)) return true;
		}
	}
	vis[u]=-1;
	return false;
}

bool neg_circle()
{
	memset(vis,-1,(n+1)*sizeof(vis[0]));
	for(int i=0;i<n;++i) dis[i]=0.0;
	for(cur=0;cur<n;++cur)
		if(dfs(cur)) return true;
	return false;
}

int main()
{
	while(scanf("%d",&n)==1&&n){
		mymap.clear();
		for(int i=0;i<1000;++i){
			ins[i].clear();
			outs[i].clear();
		}
		int t=0;
		for(int i=0;i<n;++i){
			scanf("%s",s);
			word[i].len=strlen(s);
			if(word[i].len<2) continue;
			char tmp[16];
			tmp[0]=s[0],tmp[1]=s[1],tmp[2]='\0';
		 	string x=tmp;
			if(mymap[x]==0) mymap[x]=++t;
 			tmp[0]=s[word[i].len-2],tmp[1]=s[word[i].len-1],tmp[2]='\0';
			string y=tmp;
			if(mymap[y]==0) mymap[y]=++t;
			word[i].in=mymap[x];
			word[i].out=mymap[y];
			ins[word[i].in].push_back(i);
			outs[word[i].out].push_back(i);
		}
		for(int i=0;i<n;++i){
			g[i].clear();
			int u=word[i].out;
			for(int j=ins[u].size()-1;j>=0;--j)
				g[i].push_back(ins[u][j]);
		}
		double l=0.0,r=1024.0;
		while(fabs(r-l)>1e-4){
			mid=(l+r)/2;
			if(neg_circle())
				l=mid;
			else
				r=mid;
		}
		if(abs(l)<1.0)
			printf("No solution.\n");
		else
			printf("%.2lf\n",l+1e-9);
	}
} 
时间: 2024-12-20 16:02:02

poj 2949 Word Rings 参数搜索+负环探测的相关文章

POJ 1364 King 差分约束 找负环

嘛,虽然是一道水题+模板题,不过还是学到了很多东西的,记录一下. 首先题目给出的不等式是小于,但是差分约束系统只能处理小于等于的情况,所以要转化成小于等于的进行处理.对于整数处理方法非常简单= = 然后是找负环的情况,其实不需要考虑图连不连通,只要一开始就把所有的点的d置成0,然后都push进队列里面就好了. PS:这种方法同样可以用在处理多源点最短路问题上. #include <cstdio> #include <cstring> #include <cmath> #

POJ 2679 Adventurous Driving | SPFA + 判定负环

POJ 2679 Adventurous Driving 恶心的输入恶心的题面啊...这道题学到的东西还是蛮多的 Description..有向图..边权有两个:1.费用, 2.长度.要求找出S到T花费最小的路.一定要是花费最小....在花费最小的情况下输出路径长度的最小值..然后边权可以为负..不保证S到T一定有一条路..同时点a到点b之间可能有多条路... 总之就是..S不能到T输出VOID,S到T的路径上有负环(即没有最小值)输出UNBOUND..其他有解情况输出最小花费和最小路径长度..

poj 3259 Wormholes(bellman-ford判断负环)

题目链接:http://poj.org/problem?id=3259 题目就是问你能否回到原点而且时间还倒回去了.题目中有些路中有单向的虫洞能让时间回到过去 所以只要将虫洞这条边的权值赋为负然后再判断有没有负环就行了. #include <iostream> #include <cstring> using namespace std; const int inf = 10001; int f , n , m , w ,dis[1001] , counts; struct TnT

poj 3111 K Best 参数搜索之牛顿迭代法

题意: 给n个元素,每个元素有两个属性(v,w),现在要从中选k个,使sum(v)/sum(k)最大. 分析: 参数搜索的入门题,牛顿迭代比二分快很多. 代码: //poj 3111 //sep9 #include <iostream> #include <algorithm> #include <cmath> using namespace std; const int maxN=100024; int n,k; double s0,s1; struct Node {

POJ 3259 虫洞旅行 spfa判负环

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31425   Accepted: 11431 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way p

POJ Wormholes 最短路径 ballman_ ford 有负环

1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <queue> 6 #define MAX 9999999 7 8 using namespace std; 9 10 struct node 11 { 12 int u, v, w;//u 为起点,v为终点,w为u—>v的权值 13 }; 14 n

(简单) POJ 3259 Wormholes,SPFA判断负环。

Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Eac

POJ 3259 Wormholes【Bellman_ford判断负环】

题意:给出n个点,m条正权的边,w条负权的边,问是否存在负环 因为Bellman_ford最多松弛n-1次, 因为从起点1终点n最多经过n-2个点,即最多松弛n-1次,如果第n次松弛还能成功的话,则说明存在有负环 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector&

poj 3259 Wormholes【spfa判断负环】

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36729   Accepted: 13444 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way p