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

Posted cxchanpin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
#define ll long long

#define INF2 0x03F3F3F3F
#define INF 1.0
const int N=100+10;
int path[N];
bool vis[N];
double cost[N][N],lowcost[N],h[N];
double ans;
int n,m;
void Dij()
{
    int i,j,beg=0;
    double mmin;
    memset(vis,0,sizeof(vis));
    vis[beg]=1;
    for(i=0;i<n;i++)
    {
        lowcost[i]=cost[beg][i];path[i]=beg;
    }
    lowcost[beg]=0;
    path[beg]=-1;
    int pre=beg;
    for(i=1;i<n;i++)
    {
        mmin=-1;
        for(j=0;j<n;j++)
            if(vis[j] == 0 && lowcost[pre]*cost[pre][j]>lowcost[j])
            {
                lowcost[j]=lowcost[pre]*cost[pre][j];
                path[j]=pre;
            }
        for(j=0;j<n;j++)
            if(vis[j]==0 && lowcost[j]>mmin)
            {
                mmin=lowcost[j];
                pre=j;
            }
        vis[pre]=1;
    }
}

void Init()
{
    ans=0.0;
    for(int i=0;i<=n;i++)
        for(int j=0;j<=n;j++)
            cost[i][j]=-1,lowcost[i]=0;
}

void Addedge()
{

    int u,v;
    double tmp;
    h[0]=0;
    for(int i=1;i<n;i++)
        scanf("%lf",&h[i]);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d%lf",&u,&v,&tmp);
        if(u == n)u=0;
        if(v == n)v=0;
        if(cost[u][v]<1.0-tmp)//////////
        cost[u][v]=cost[v][u]=1.0-tmp;
    }
}
/*double dfs(int i,double ret)
{
    if(path[i])return dfs(path[i],ret)*cost[path[i]][i];
    return ret*cost[0][i];
}*/
int main()
{
    //freopen("zoj1655.txt","r",stdin);
    while(~scanf("%d%d",&n,&m))
    {
        Init();
        Addedge();
        Dij();
        for(int i=1;i<n;i++)
        {
            //ans+=dfs(i,h[i]);
            ans+=h[i]*1.0*lowcost[i];
        }
        printf("%.2lf\n",ans);
    }
    return 0;
}



以上是关于zoj 1655 单源最短路 改为比例+最长路的主要内容,如果未能解决你的问题,请参考以下文章

单源最短路径

贪心算法(Dijkstra)解决单源最短路径问题(C++)

单源最短路径_贪心算法

poj3268

[单源最短路]逃离僵尸岛

poj1502 MPI Maelstrom(单源最短路)