标签:
给一个n个点m条边的图,每一个点和每一条边都有权值。现在要找一个环的点权和/边权和最大,求这个最大值。
SPFA+二分
题目的关系式:点权和/边权和 <= ans
转换一下就变成 ans*边权和 - 点权和 >= 0;
二分答案,然后用SPFA去check是否存在一个负权回路。
/*
#pragma warning (disable: 4786)
#pragma comment (linker, "/STACK:0x800000")
*/
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <iterator>
#include <utility>
using namespace std;
template< class T > T _abs(T n)
{
return (n < 0 ? -n : n);
}
template< class T > T _max(T a, T b)
{
return (!(a < b) ? a : b);
}
template< class T > T _min(T a, T b)
{
return (a < b ? a : b);
}
template< class T > T sq(T x)
{
return x * x;
}
template< class T > T gcd(T a, T b)
{
return (b != 0 ? gcd<T>(b, a%b) : a);
}
template< class T > T lcm(T a, T b)
{
return (a / gcd<T>(a, b) * b);
}
template< class T > bool inside(T a, T b, T c)
{
return a<=b && b<=c;
}
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define F(i, n) for(int (i)=0;(i)<(n);++(i))
#define rep(i, s, t) for(int (i)=(s);(i)<=(t);++(i))
#define urep(i, s, t) for(int (i)=(s);(i)>=(t);--(i))
#define repok(i, s, t, o) for(int (i)=(s);(i)<=(t) && (o);++(i))
#define MEM0(addr) memset((addr), 0, sizeof((addr)))
#define MP(x, y) make_pair(x, y)
#define REV(s, e) reverse(s, e)
#define SET(p) memset(pair, -1, sizeof(p))
#define CLR(p) memset(p, 0, sizeof(p))
#define MEM(p, v) memset(p, v, sizeof(p))
#define CPY(d, s) memcpy(d, s, sizeof(s))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define SZ(c) (int)c.size()
#define PB(x) push_back(x)
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pii pair< int, int >
#define psi pair< string, int >
#define ls u << 1
#define rs u << 1 | 1
#define lson l, mid, u << 1
#define rson mid, r, u << 1 | 1
#define debug(x) cout << #x << " = " << x << endl
#define eps 1e-3
const int MAXN = 1111;
const int maxnum = 100010;
const int INF = 0x3f3f3f3f;
const int mod = 1e9;
using namespace std;
int m,n;
int head[MAXN];
double dis[MAXN];
int vis[MAXN],cost[MAXN];
int value[MAXN];
int _count[MAXN];
int cnt = 0;
struct node{
int to;
int next;
int w;
}edge[MAXN * 10];
void init(){
MEM(head,-1);
cnt = 0;
}
void addedge(int u,int v,int w){
edge[cnt].to = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
}
int SPFA(double mid){
MEM0(vis);
MEM0(_count);
rep(i,1,n) dis[i] = INF;
dis[1] = 0;
queue<int> Q;
Q.push(1);
while(!Q.empty()){
int u = Q.front();
Q.pop();
vis[u] = 0;
_count[u]++;
if(_count[u] > n) return 1;
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].to,w = edge[i].w;
double tmp = mid * w - value[v];
if(dis[u] + tmp < dis[v]){
dis[v] = dis[u] + tmp;
if(!vis[v]){vis[v] = 1; Q.push(v);}
}
}
}
return 0;
}
void solve(){
double l = 0,r = 10000,mid, ans = 0;
while(r - l > eps){
mid = (l + r) / 2;
if(SPFA(mid)){
ans = mid;
l = mid - 0.000001;
}else
r = mid + 0.000001;
}
printf("%.2f\n", ans);
}
int main(){
//READ("in.txt");
while(scanf("%d%d",&n,&m)!=EOF){
init();
rep(i,1,n) scanf("%d",&value[i]);
while(m--){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
}
solve();
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3621(0/1分数规划,二分) Sightseeing Cows
标签:
原文地址:http://blog.csdn.net/notdeep__acm/article/details/47345767