ZOJ--1655--Transport Goods【dijkstra】

题意:某国首都正被攻打,需要运送物资到首都,告诉你n个点,编号1~n,n是首都,剩下的点各有wi重量的物资,m条路,每条路有个货物损失比例,现需要求出最多能运送多少货物到首都。

其实转换一下就是一个最短路问题,边的权值是损失比例,找损失比例最小的那条路,则能运送的货物最多。

dist数组存放运成功的比例,初始化为0表示运不成。

WA了N发,各种double类型都用int定义的,而且它给的样例即使定义成int对结果也没影响。。。

#include<cstring>
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#include<functional>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
#define MAXN 1010
#define eps 1e-11
#define INF 0x7FFFFFFF
#define long long ll;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

double edge[105][105];
double dist[105];
int vis[105];
int w[105];
int n, m;
void dijkstra(int v){
    int i, j;
    memset(vis,0,sizeof(vis));
    memset(dist,0,sizeof(dist));
    vis[v] = 1;
    dist[v] = 1;
    int k = v;
    for(i=0;i<n;i++){
        for(j=1;j<=n;j++){
            if(!vis[j]&&edge[k][j]!=-1&&dist[j]<dist[k]*edge[j][k]){
                dist[j] = dist[k] * edge[j][k];
            }
        }
        double temp = 0;
        for(j=1;j<=n;j++){
            if(!vis[j]&&dist[j]-temp>eps){
                k = j;
                temp = dist[j];
            }
        }
        vis[k] = 1;
    }
}
int main(){
    int i, j, a, b;
    double c, ans;
    while(scanf("%d%d",&n,&m)!=EOF){
        ans = 0;
        for(i=0;i<n-1;i++){
            scanf("%d",&w[i+1]);
        }
        for(i=1;i<=n;i++){
            for(j=1;j<=n;j++){
                edge[i][j] = -1;
            }
        }
        for(i=0;i<m;i++){
            scanf("%d%d%lf",&a,&b,&c);
            if(1-c>edge[a][b])
                edge[a][b] = edge[b][a] = 1 - c;
        }
        dijkstra(n);
        for(i=1;i<n;i++)    ans += dist[i]*w[i];
        printf("%.2lf\n",ans);
    }
    return 0;
}

ZOJ--1655--Transport Goods【dijkstra】

时间: 2024-10-04 03:05:20

ZOJ--1655--Transport Goods【dijkstra】的相关文章

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 Transport Goods 最短路的应用

这道题目真是充分显示了窝的智商低下,首先题目在有中文翻译的情况下看了半天没看懂,= = 然后已知这题的分类是最短路了还是不会做,= = 一开始想着在dij扩展的的时候就把最大运送值算好,不过后来发现正向运输和反向运输的值显然是不相等的o(╯□╰)o 后来直接把每个城市作为起点dij了,反正n就一百,n^3就n^3了,后来发现可以从多条路一起送到终点 无奈看了题解,发现原来dij算的应该是终点到各个点的最小费率(最大比率),这样不仅正向反向算结果是一样的,而且不会漏. 话说题目本来就是要把每个城市

HDU2680 Choose the best route 【Dijkstra】

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7061    Accepted Submission(s): 2300 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

HDU1385 Minimum Transport Cost 【Floyd】+【路径记录】

Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7496    Accepted Submission(s): 1918 Problem Description These are N cities in Spring country. Between each pair of cities

HDU1874 畅通工程续 【Dijkstra】

畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 26970    Accepted Submission(s): 9719 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走

HDU2544 最短路 【Dijkstra】

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 31182    Accepted Submission(s): 13456 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

HDOJ 1142 A Walk Through the Forest 【Dijkstra】+【DFS】

题意:从2到1的所有路径中找出最短的路,并且输出最短路径有几条. 策略:先求出最短路径,然后再找出从2到1的最短路径有几条.最短路径用dijkstra算法来求出,什么是dijkstra算法,简单来说,dijkstra算法就是路径长度递增次序产生最短路径的算法: 基本思想是:把集合V分成两组: (1)S:已求出最短路径的顶点的集合 (2)V-S=T:尚未确定最短路径的顶点集合 将T中顶点按最短路径递增的次序加入到S中, 保证:(1)从源点V0到S中各顶点的最短路径长度都不大于 从V0到T中任何顶点

HDOJ 1874 畅通工程续 【dijkstra】

题意:... 策略:最简单的求最短路径. 代码: #include<stdio.h> #include<string.h> #define MAXN 1005 #define INF 0x3f3f3f3f int di[MAXN], vis[MAXN], n, m; int map[MAXN][MAXN]; void dijkstra(int v) { int i, j; memset(vis, 0, sizeof(vis)); di[v] = 0; vis[v] = 1; for