Zoj 1655 Transport Goods 最短路的应用

这道题目真是充分显示了窝的智商低下,首先题目在有中文翻译的情况下看了半天没看懂,= =

然后已知这题的分类是最短路了还是不会做,= =

一开始想着在dij扩展的的时候就把最大运送值算好,不过后来发现正向运输和反向运输的值显然是不相等的o(╯□╰)o

后来直接把每个城市作为起点dij了,反正n就一百,n^3就n^3了,后来发现可以从多条路一起送到终点

无奈看了题解,发现原来dij算的应该是终点到各个点的最小费率(最大比率),这样不仅正向反向算结果是一样的,而且不会漏。

话说题目本来就是要把每个城市里面的物品全部运送到终点的,只是对于每个城市选一条费率最小的边而已,这不是裸的dij么,这都不会,sad...

PS:这题有重边,WA哭TAT

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>

using namespace std;

typedef long long LL;
const int INF = INT_MAX / 4;
const int maxn = 105;
int vis[maxn],n,m,w[maxn];
double p[maxn][maxn],d[maxn];

void dijkstra() {
    memset(vis,0,sizeof(vis));
    for(int i = 1;i <= n;i++) {
        d[i] = -1;
    }
    d[n] = 1;
    for(int k = 1;k <= n;k++) {
        double maxv = -1; int x = n;
        for(int i = 1;i <= n;i++) if(!vis[i] && d[i] > maxv) {
            maxv = d[x = i];
        }
        vis[x] = true;
        for(int i = 1;i <= n;i++) if(!vis[i] && p[x][i] >= 0) {
            if(d[i] == -1) d[i] = d[x] * p[x][i];
            else d[i] = max(d[i],d[x] * p[x][i]);
        }
    }
}

int main() {
    while(scanf("%d%d",&n,&m) != EOF) {
        for(int i = 1;i < n;i++) {
            scanf("%d",&w[i]);
        }
        for(int i = 1;i <= n;i++) {
            for(int j = 1;j <= n;j++) {
                p[i][j] = -5;
            }
        }
        for(int i = 1;i <= m;i++) {
            int a,b;
            double c; scanf("%d%d%lf",&a,&b,&c);
            p[a][b] = p[b][a] = max((1 - c),p[a][b]);
        }
        dijkstra();
        double ans = 0;
        for(int i = 1;i < n;i++) if(d[i] > 0) {
            ans += d[i] * w[i];
        }
        printf("%.2f\n",ans);
    }
    return 0;
}

  

Zoj 1655 Transport Goods 最短路的应用

时间: 2024-10-09 01:04:18

Zoj 1655 Transport Goods 最短路的应用的相关文章

zoj 1655 Transport Goods (最短路变形)

Transport Goods Time Limit: 2 Seconds      Memory Limit: 65536 KB The HERO country is attacked by other country. The intruder is attacking the capital so other cities must send supports to the capital. There are some roads between the cities and the

ZOJ 1655 Transport Goods

The HERO country is attacked by other country. The intruder is attacking the capital so other cities must send supports to the capital. There are some roads between the cities and the goods must be transported along the roads. According the length of

zoj 1655 单源最短路 改为比例+最长路

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=655 没有理解清题意就硬套模板,所以WA了好几次, 解析看我的另一篇http://blog.csdn.net/u011026968/article/details/35579035 贴代码 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #de

hdu1385Minimum Transport Cost(最短路变种)

题目链接: huangjing 思路: 输出路径的最短路变种问题..这个题目在于多组询问,那么个人觉得用floyd更加稳妥一点.还有就是在每个城市都有过路费,所以在floyd的时候更改一下松弛条件即可..那么输出路径怎么办呢??我采用的是输出起点的后继而不是终点的前驱..因为我们关心的是路径字典序最小,关心的是起点的后继...那么打印路径的时候就直接从前向后打印,这个和dijkstra的打印路径稍有不同...最短路的打印参见传送门 题目: Minimum Transport Cost Time

ZOJ - 3794 Greedy Driver 最短路

首先正向跑一遍1为起点的最短路,注意松弛过程如果走到加油站则dis=0,并且路上任意时刻dis都不能大于C,判断dis[n]是否<=C就能判断无解情况了. 然后反向建图再跑一次N为起点的最短路,这样可以求到每个点到n点的最短路. 对于每一个可以交易的城市,C-dis1[i]-dis2[i]就是多出来可以卖掉的油. #include<iostream> #include<cstdlib> #include<cstring> #include<cmath>

zoj 2027 Travelling Fee (最短路变形)

Travelling Fee Time Limit: 2 Seconds      Memory Limit: 65536 KB Samball is going to travel in the coming vacation. Now it's time to make a plan. After choosing the destination city, the next step is to determine the travel route. As this poor guy ha

zoj 1221 Risk【最短路 floyd】

ZOJ Problem Set - 1221 Risk Time Limit: 2 Seconds      Memory Limit: 65536 KB Risk is a board game in which several opposing players attempt to conquer the world. The gameboard consists of a world map broken up into hypothetical countries. During a p

HDU - 1385 Minimum Transport Cost(最短路+最小字典序路径)

题目大意:有N个村庄.过村庄时需要交一定的费用.现在问从村庄A,要运一批货物到村庄B,怎样走才能使费用达到最小,起始和终点都不用缴费 解题思路:这题借鉴了别人的思路,用字符串存储路径. 其实不用字符串也是可以处理的 #include <cstdio> #include <cstring> #include <queue> using namespace std; #define N 105 #define INF 0x3f3f3f3f struct Node { cha

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i