POJ3621 Sightseeing Cows【最短路】

题目大意:在一个无向图里找一个环,是的点权和除以边权和最大

思路:UVA11090姊妹题 事实上当这题点权和都为1时就是上一题TUT

#include <stdio.h>

#include <iostream>

#include<queue>

#include <string.h>

#include <algorithm>

#define maxn 10009

#define maxm 10001

#define esp 0.001

using namespace std;

int head[1009],next[maxn],now=0,value1[maxn];

int point[maxn],inque[1009],n,m,f[1009];

double dist[1009];

void add(int x,int y,int u,int v)

{

next[++now]=head[x];

head[x]=now;

point[now]=y;

value1[now]=u;

}

int spfa(int s,double ans)

{

memset(inque,0,sizeof(inque));

for(int i=1;i<=n+1;i++)dist[i]=0x3f3f3f3f;

dist[s]=0;

int visit[maxn]={0};

visit[s]=1;

queue<int>q;

q.push(s);

while(!q.empty())

{

int u=q.front();

q.pop();

visit[u]=0;

for(int i=head[u];i;i=next[i])

{

int k=point[i];

double vv=ans*value1[i]-f[k];;

if(dist[u]+vv<dist[k])

{

dist[k]=dist[u]+vv;

if(visit[k]==0)

{

visit[k]=1;

inque[k]++;

if(inque[k]>=n)return 1;

q.push(k);

}

}

}

}

return 0;

}

int main()

{

double l=0,r=2000;

int x,y,v;

scanf("%d%d",&n,&m);

for(int i=1;i<=n;i++)

{

//    add(n+1,i,0,0);

scanf("%d",&f[i]);

}

for(int i=1;i<=m;i++)

{

scanf("%d%d%d",&x,&y,&v);

add(x,y,v,f[y]);

//  add(y,x,v,f[x]);

//  r+=v;

}

while(r-l>esp)

{

double mid=(l+r)/2;

if(spfa(1,mid)==0)r=mid;else l=mid;

}

//   printf("%d\n",spfa(1,6.128));

printf("%.2f\n",l);

return 0;

}

时间: 2024-10-25 14:53:55

POJ3621 Sightseeing Cows【最短路】的相关文章

poj3621 Sightseeing Cows --- 01分数规划

典型的求最优比例环问题 参考资料: http://blog.csdn.net/hhaile/article/details/8883652 此题中,给出每个点和每条边的权值,求一个环使 ans=∑点权/∑边权 最大. 因为题目要求一个环,而且必然是首尾相接的一个我们理解的纯粹的环,不可能是其他样子的环, 所以我们可以把一条边和指向的点看做整体处理. 上面方程可以化为:ans×e[i]-p[i]=0 以它为边权二分答案,spfa求负环,有负环则该ans可行,增大下界. 若一直不可行,则无解. #i

01分数规划+spfa判负环 POJ3621 Sightseeing Cows

Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10348   Accepted: 3539 Description Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to

POJ3621 Sightseeing Cows 最优比率环 二分法

题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10552   Accepted: 3613 Description Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big ci

poj3621 Sightseeing Cows

01分数规划 二分+spfa负环(SLF优化) #include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; int n,m; double p[1100]; struct edge{int x,y;double d;}e[5100]; st

【POJ3621】Sightseeing Cows 分数规划

[POJ3621]Sightseeing Cows 题意:在给定的一个图上寻找一个环路,使得总欢乐值(经过的点权值之和)/ 总时间(经过的边权值之和)最大. 题解:显然是分数规划,二分答案ans,将每条边的权值变成(ans*边权-2*起始点点权),然后我们希望找出一个环,使得环上的总边权<0 (一开始我把题意理解错了,题中给的是单向边,我把它当成是双向边+每条边只能走一次了~,想出一堆做法都接连pass掉) 然后就直接用SPFA判负环就好了嘛!由于原图不一定联通,所以一开始就把所有点都入队就完事

【POJ3621】Sightseeing Cows

Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8331   Accepted: 2791 Description Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to

POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】

题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11526   Accepted: 3930 Description Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big ci

poj 3463 Sightseeing(最短路+次短路)

http://poj.org/problem?id=3463 大致题意:给出一个有向图,从起点到终点求出最短路和次短路的条数之和. 解法: 用到的数组:dis[i][0]:i到起点的最短路,dis[i][1]:i到起点的严格次短路 vis[i][0],vis[i][1]:同一维的vis数组,标记距离是否已确定 sum[i][0]:i到起点的最短路条数,sum[i][1]:i到起点的次短路条数 同一维dijkstra,内循环先找出最短的距离(次短路或最短路)d,然后枚举与该点相连的点: if(d

POJ 3621 Sightseeing Cows(最优比例环+SPFA检测)

Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10306   Accepted: 3519 Description Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big city! The cows must decide how best to