日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Sightseeing Cows(POJ-3621)

發(fā)布時間:2025/3/17 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Sightseeing Cows(POJ-3621) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Problem 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 spend their free time.

Fortunately, they have a detailed city map showing the L (2 ≤ L ≤ 1000) major landmarks (conveniently numbered 1.. L) and the P (2 ≤ P ≤ 5000) unidirectional cow paths that join them. Farmer John will drive the cows to a starting landmark of their choice, from which they will walk along the cow paths to a series of other landmarks, ending back at their starting landmark where Farmer John will pick them up and take them back to the farm. Because space in the city is at a premium, the cow paths are very narrow and so travel along each cow path is only allowed in one fixed direction.

While the cows may spend as much time as they like in the city, they do tend to get bored easily. Visiting each new landmark is fun, but walking between them takes time. The cows know the exact fun values Fi (1 ≤ Fi ≤ 1000) for each landmark i.

The cows also know about the cowpaths. Cowpath i connects landmark L1i to L2i (in the direction L1i -> L2i ) and requires time Ti (1 ≤ Ti ≤ 1000) to traverse.

In order to have the best possible day off, the cows want to maximize the average fun value per unit time of their trip. Of course, the landmarks are only fun the first time they are visited; the cows may pass through the landmark more than once, but they do not perceive its fun value again. Furthermore, Farmer John is making the cows visit at least two landmarks, so that they get some exercise during their day off.

Help the cows find the maximum fun value per unit time that they can achieve.

Input

* Line 1: Two space-separated integers: L and P
* Lines 2..L+1: Line i+1 contains a single one integer: Fi
* Lines L+2..L+P+1: Line L+i+1 describes cow path i with three space-separated integers: L1i , L2i , and Ti

Output

* Line 1: A single number given to two decimal places (do not perform explicit rounding), the maximum possible average fun per unit time, or 0 if the cows cannot plan any trip at all in accordance with the above rules.

Sample Input

5 7
30
10
10
5
10
1 2 3
2 3 2
3 4 5
3 5 2
4 5 5
5 1 3
5 2 2

Sample Output

6.00

題意:給出 n 個點 m 條邊的有向圖,每個點和每條邊都有自己的權(quán)值,求一個環(huán),使得環(huán)中各點點值與各邊邊值的和的比率最大,求這個比率

思路:01 分?jǐn)?shù)規(guī)劃中的最優(yōu)比率環(huán)問題,利用 SPFA 在判斷負(fù)環(huán)的過程中對邊進行重賦值,從而利用二分法來二分答案

具體思路:點擊這里

Source Program

#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #include<bitset> #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LL long long #define Pair pair<int,int> LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; } LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;} LL quickMultPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;} LL quickPowMod(LL a,LL b,LL mod){ LL res=1; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1; } return res; } LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); } LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); } LL LCM(LL x,LL y){ return x/GCD(x,y)*y; } const double EPS = 1E-6; const int MOD = 1000000000+7; const int N = 1000+5; const int dx[] = {0,0,-1,1,1,-1,1,1}; const int dy[] = {1,-1,0,0,-1,1,-1,1}; using namespace std;struct Edge {int to, next;double cost; //邊權(quán) } edge[N * 10]; int head[N], tot; int n, m; double value[N]; //點權(quán) double dis[N]; bool vis[N]; int cnt[N]; //進隊次數(shù) void addEdge(int x, int y, double w) {edge[tot].to = y;edge[tot].next = head[x];edge[tot].cost = w;head[x] = tot++; } int SPFA(double x) {memset(dis, 0x43, sizeof(dis));memset(cnt, 0, sizeof(cnt));memset(vis, false, sizeof(vis));dis[1] = 0;cnt[1]++;queue<int> Q;Q.push(1);while (!Q.empty()) {int u = Q.front();Q.pop();vis[u] = 0;for (int i = head[u]; i != -1; i = edge[i].next) {int v = edge[i].to;double val = edge[i].cost * x - value[v]; //重賦值if (dis[v] > dis[u] + val) {dis[v] = dis[u] + val;if (!vis[v]) {vis[v] = true;cnt[v]++;if (cnt[v] >= n)return true;Q.push(v);}}}}return false; } int main() {tot = 0;memset(head, -1, sizeof(head));scanf("%d%d", &n, &m);for (int i = 1; i <= n; i++) //點權(quán)scanf("%lf", &value[i]);for (int i = 1; i <= m; i++) {int x, y;double w;scanf("%d%d%lf", &x, &y, &w);addEdge(x, y, w);}double left = 0, right = 10000;while (right - left >= EPS) {double mid = (left + right) / 2.0;if (SPFA(mid)) //存在負(fù)環(huán),更改下界left = mid;elseright = mid;}printf("%.2f\n", left);return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的Sightseeing Cows(POJ-3621)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。