POJ 2949

POJ-2949 Word Rings(SPFA DFS实现 + 环上平均权值最大) Description A word ring is a sequence of words where the last two letters of each word are the same as the first two letters of the next word (and the last two letters of the last word are the same a 阅读详情

DFS的SPFA用于判负环

#include<cstring>
#include<cstdio>
#define N 200100
using namespace std;
int n;
int vis[900],k;
int head[N],cnt;
double dis[N],mid;
bool is[N];
int flag;
struct Edge{
    int v,w,next;
}edge[N];
void init(){
    memset(vis,0,sizeof(vis));
    memset(head,-1,sizeof(head));
    k=cnt=0;
}
void addedge(int u,int v,int w){
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void dfs(int u){
    int i;
    is[u]=1;
    for(i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].v;
        if(dis[v]+1e-8<dis[u]+(double)edge[i].w-mid){
            dis[v]=dis[u]+(double)edge[i].w-mid;
            if(is[v]){
                flag=1;
                return;
            }
            dfs(v);
            if(flag)return;
        }
    }
    is[u]=0;
}
bool judge(){
    int i;
    for(i=1;i<=n;i++)dis[i]=0;
    memset(is,0,sizeof(is));
    flag=0;
    for(i=1;i<=n;i++){
        dfs(i);
        if(flag)return 1;
    }
    return 0;
}
int main(){
    int i,j,len;
    char str[1100];
    while(scanf("%d",&n)){
        if(!n)break;
        init();
        for(i=1;i<=n;i++){
            scanf("%s",str);
            len=strlen(str);
            if(len==1)continue;
            if(vis[(str[0]-'a')+(str[1]-'a')*26]==0)
                vis[(str[0]-'a')+(str[1]-'a')*26]=++k;
            if(vis[(str[len-2]-'a')+(str[len-1]-'a')*26]==0)
                vis[(str[len-2]-'a')+(str[len-1]-'a')*26]=++k;
            addedge(vis[(str[0]-'a')+(str[1]-'a')*26],vis[(str[len-2]-'a')+(str[len-1]-'a')*26],len);
        }
        double h=1001,l=0;
        bool ok=0;
        while(h-l>1e-8){
            mid=(h+l)/2;
            if(judge()){
                ok=1;
                l=mid;
            }
            else h=mid;
        }
        if(ok==0)printf("No solution.\n");
        else printf("%.2f\n",h+1e-8);
    }
}


Word Rings POJ2949 (Bellman-ford算法,找平均值最大的环) 题意本身比较简单,就是找一个平均值最大的环。 在做这个题之前,我们先解决一个问题,如何找到一个平均值最小的环。设有一个环中,有k 个点,那么这个环的平均权值X是: X=(w1+w2+w3+,..,,,wk)/k 然后我们把k乘过去:(w1+w2+…..wk)=kXBellman_ford算法的一个重要作用是判断负圈,所以这个题可以利用这个特点来做。 再把(w1+w2+…..wk)=kX处理一 阅读详情

相关推荐

vscode使用continue插件连接LM studio的模型

本文详细介绍了如何使用VSCode的Continue插件连接LM Studio模型。主要步骤包括:在LM Studio中加载模型并设置相关参数,启动API服务,修改Continue配置文件以集成LM Studio模型,并进行测试以验证连接是否成功。通过这些步骤,用户可以将LM Studio中的预训练模型与VSCode的Continue插件无缝对接,实现高效的自然语言处理和编程辅助功能。

weixin_43590837的博客 8643

POJ 2949(二分+dfs)

POJ 2949 (1)思路: 将单词的前两个,后两个的单词视为点,将单词的长度视为边长,建立一个有向图, 然后深搜求出有向图的环的长度。 由于有向图的平均长度未知,可以将一下公式化简一下: (E1+E2+E3+E4+……+Ek)/k = ave; (E1+E2+E3+E4+……+Ek) = ave*k; (E1-ave*k)+(E2-ave*k)+(E3-ave*k)+……+...

积少成多,水滴石穿 394

基于springboot生鲜食品订购系统的设计与实现

近年来互联网络的迅猛发展和电子终端设备的普及,赋予了各行业充足的发展空间。微信小程序的O2O生鲜食品订购相比于传统信息技术,时效性是它最大的特色,已经在电子娱乐、经济等中发挥着举足轻重的作用。短时间内迅速扩大了线上管理系统的规模。尽管O2O生鲜食品订购已经有了很大程度的发展,但在微信小程序的O2O生鲜食品订购领域上却少有建设。

1028

POJ 2949 Word Rings(差分约束)

思路:找平均值最大的环,将字符串前两个和最后两个当做两个结点,有一条边权值就是这个串的长度。            那么avg = (w1+w2+....wk)/k,把k乘过去有avg*k = (w1+w2+...wk),然后有(w1-avg)+(w2-avg)+...(wk-avg)>=0,然后其实就是要求(w1-ans)+(w2-ans)+...(wk-ans)>0,二分ans然后求正环。

围巾的ACM博客 760

Microsoft Visual C++ 14.0 is required 的亲测最全解决方案(包括安装包丢失或损坏解决方法)

在用python调用别的语言(我用的是cache数据库)的方法时,在执行"python setup3.py install"方法时出现“error: Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visual C++ Build Tools”错误,以下是网上能找到的安装过程中“所有”解决方案: 1、首先需要下载Micr...

CDL2017CDL的博客 2万+

计算机视觉(相机标定;内参;外参;畸变系数)

目录 一、预备知识 1、坐标系变换过程(相机成像过程) (1)相机坐标系转换为图像坐标系(透视投影变换遵循的是针孔成像原理) (2)齐次坐标的引入原因:(为什么引入齐次坐标???) 2、内参与外参矩阵的构成 3、畸变参数 二、相机标定 1、张正友标定法(光学标定)及其求解思路 2、求解内参矩阵与外参矩阵的思路: (1)计算内参与外参矩阵的乘积M (2)求解内参矩阵A (3)求解外参矩阵(R1 R2 T) 三、参数优化 1、L-M算法 2、相机标定步骤 四、相关代码及其实验结.

QYJ_WORKHARDING的博客 6万+

POJ 2949 SPFA

对一个权值可正可负的图SPFA判是否存在正环 首先引用一篇国家集训队论文《spfa的优化及应用》——姜碧野 给个百度传送门http://wenku.baidu.com/view/9e1231126edb6f1aff001f25.html orz POJ 2949(先将该题题意转化到二分答案判正环这一步)//node为点的个数, //基础代码5000+ms危险过 bool s

chm517的专栏 846

NE564锁相环FM解调电路设计:10.7MHz中频实测与3种失真优化方案

本文详细解析了NE564锁相环在10.7MHz中频FM解调电路中的设计与优化方案。通过分析锁相环技术基础、电路架构设计及典型失真问题,提供了3种有效的失真优化方法,包括限幅增强、VCO频偏补偿和环路滤波优化,显著提升解调性能。文章还涵盖了PCB布局要点和系统测试验证,为高频电子线路设计提供实用参考。

weixin_33314040的博客 357

poj2949 Word Rings(建图+二分答案+spfa判正环)

把每个单词视为一条边,由开头两字母指向结尾两字母,边权为单词长度。则原问题转化为求一个最优比率环,可以利用二分答案+spfa判环来解决。点最多有26*26个,边最多1e5.dfs版spfa判环就是快。。

Icefox的博客 1292

CATIA高效操作:自定义快捷键全攻略

本文详细介绍了CATIA软件中自定义快捷键的完整攻略,旨在帮助工程师显著提升三维设计效率。文章从设置入口、实战操作步骤讲起,并提供了遵循左手键盘右手鼠标原则的黄金法则、十大必设快捷键推荐以及管理备份等进阶技巧,指导用户通过合理设置和刻意练习形成肌肉记忆,让CATIA操作更加流畅高效。

weixin_29247219的博客 404

UG\NX二次开发 分割曲线

分割曲线没有ufun函数?那就换一种思路实现。

王牌飞行员_里海的博客 1153

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

类似poj 3621。 代码: //poj 2949 //sep9 #include #include #include #include #include using namespace std; const int maxL=1024; const int maxM=100100; char s[maxL]; struct Words { int in,out,len; }w

sepNINE的专栏 1103

Java使用POI生成柱状图导出到word文档(柱状图)

本篇文章主要介绍,如何使用Apache POI组件生成柱状图导出到word文档中,具体步骤看下文。 一、实现效果 Java使用POI技术生成柱状图导出到word文档中,最终生成的柱状图如下所示: 二、环境准备 编程语言:Java 第三方依赖:Apache POI Apache POI依赖代码如下所示: <dependencies> <dependency> <groupId>org.apache.poi</groupId.

✅ Java 开发工程师 7398

poj 2949

#include<iostream>#include<fstream>using namespace std;int n;struct e{ int s,t; double w;};e edge[100001];char c[1002];int mid;int top;void add(int s,int t,int w){ edge[++top].s=s...

weixin_30703911的博客 189

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

poj2949 word rings

http://www.elijahqi.win/archives/1168 Description A word ring is a sequence of words where the last two letters of each word are the same as the first two letters of the next word (and the last two...

Elijahqi 294

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

poj2949-Word Rings

题意:给定一个图,

No13507Oier的专栏 339

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

poj 2949Word Rings

题目点这里 大意:一个有向有环图,求最大平均环,即一个环有n个节点,权值和为sum,求最大的sum/n 二分答案,将所有边都减去平均值,然后跑spfa看有没有正环

我们做的队 329

POJ2949-平均最短路SPFA

/* 枚举每一个平均长度mid值。 如果存在一个环,(E1+...+Ek)/k>=mid(其中k是边数,E1……Ek是各个边权), 那么正解比mid大,否则比mid小,这就是二分策略。 那么怎样知道是否存在(E1+...+Ek)/k>=mid 呢? 如下转化:(E1+...+Ek)>=mid*k E1-mid + E2–mid + E3-mid + ... + Ek-mid >= 0 所以,把所有

Wo xi Meiz 1184

POJ - 2949 Word Rings(SPFA判正环,01分数规划)

http://poj.org/problem?id=2949 #include<cstdio> #include<cstring> #include<vector> #include<queue> using namespace std; typedef pair<int,int> PII; const int N = 710; vector<PII> g[N]; double dist[N]; int cnt[N]; bool s

00后的博客 386

2018.09.09 poj2949Word Rings(01分数规划+spfa判环)

传送门 这题要先巧妙的转化一下。 对于每个字符串,我们把头尾的两个小字符串对应的点连边,边权是这个字符串的长度。 这样最多会出现26*26个点。 这个时候就只用求出边权和跟边数的最大比值了。 这个显然01分数规划+spfa判环解决。 注意用dfs版的spfa要快一些。 代码: #include&lt;iostream&gt; #include&lt;cstdio&gt; #in...

苟为蒟蒻又何妨 279

poj2949 Word Rings(图论/spfa判正环 最大平均环)

题目 n(n<=1e5)个纯小写字母组成的单词,第i个单词长为|si|(|si|<=1e3) 你可以取一个或一些单词,使得第一个单词的末两位与第二个单词的首两位相同, 第二与第三,……,最后一个的末两位与第一个的首两位相同 每个单词只能用一次,假设你取了k个单词,单词总长是len,输出最大化的len/k的值 题解 单纯想总结一下,最大平均环问题 将每个单词最开始的两个字母...

小衣同学的博客 459

SPFA(判负环) + 二分(01分数规划) - Word Rings - POJ 2949

SPFA(判负环) + 二分(01分数规划) - Word Rings - POJ 2949 题意: 我们有 n 个字符串,每个字符串都是由 a∼z 的小写英文字母组成的。 如果字符串 A 的结尾两个字符刚好与字符串 B 的开头两个字符相匹配,那么我们称 A 与 B 能够相连(注意:A 能与 B 相连不代表 B 能与 A 相连。 我们希望从给定的字符串中找出一些,使得它们首尾相连形成一个环串(一个串首尾相连也算),我们想要使这个环串的平均长度最大。 如下例: ababc bckjaca caahoynaab

njuptACMcxk的博客 267

[最小环 最小环计数 最大平均环 模板题] POJ 1734 Sightseeing trip & FZU 2090 旅行社的烦恼 & POJ 2949 Word Rings

不多说 网上有很多#include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #define cl(x) memset(x,0,sizeof(x)) using namespace std; const int N=105; const int oo=1<<29; int n,m,Ans; int

雯舞 687

UVALive - 2949 Elevator Stopping Plan

把我坑惨了。。。 首先是

cgjiayou 524
上一篇: HDU 4005
下一篇: TC SRM 557 DIV2 1000
Benzema67
博客等级 码龄15年 120粉丝 357原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值