码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-06-20 13:39:12      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:ack   double   article   const   fine   sdn   题意   details   tmp   

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 单源最短路 改为比例+最长路

标签:ack   double   article   const   fine   sdn   题意   details   tmp   

原文地址:http://www.cnblogs.com/cxchanpin/p/7053587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!