zoj-1655. Use the dij function to get it directly!

http://http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=655

The meaning of the title: The idea was not very clear at the beginning, but after careful thinking, I slowly got a little idea! ! The main point here is that it is necessary to say: There are n cities, there are m roads, and the following is the weight of n-1 items (also representing n-1 cities), which are all sent to city n, and the next The data is x, y, z, which represents the consumption from city x to city Y (consumption rate z).. What is the maximum weight of items that can be received in city n? ?

Specific ideas: According to the meaning of the question, it is easy to think of DIJstra. But there is a little hindrance. It is that the distance (weight) of each segment is unknown. And the consumption rate of each segment is different, so we need to convert it, which is to find the maximum weight from each city to n cities. Add them up one by one, and you will get what you want!

See the code for details:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
double  dis[105][105];
//int map[105][105];
int val[105];
bool flag[105];
int n,m;
void dijstra()
{
    int i,j,k;
    double maxn;
    int t=n;
    memset(flag,false ,sizeof(flag));
    flag[n]=true;
    for(i=n;i>0;i--)
   {
        maxn=-1.0;
        for(j=1;j<n;j++)
        {
           if(!flag[j]&&maxn<dis[n][j])
           {
               maxn=dis[n][j];
               k=j;
           }
        }
        if(maxn==-1.0)break;
        flag[k]=true;
        for(i=1;i<n;i++)
        {
            if(!flag[i]&&dis[n][i]<dis[n][k]*dis[k][i])
            {
                dis[n][i]=dis[n][k]*dis[k][i];
            }
        }
    }
}
int main()
{
    int i;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(dis,0,sizeof(dis));
        for(i=1;i<=n-1;i++)
        {
          scanf("%d",&val[i]);
        }
        int x,y;
        double z;
        for(i=1;i<=m;i++)
        {
            scanf("%d %d %lf",&x,&y,&z);
            if(dis[x][y]<1.0-z)
                dis[x][y]=dis[y][x]=1.0-z;
        }
        dijstra();
        double ans=0;
        for(i=1;i<=n;i++)
        {
            ans+=dis[n][i]*val[i];
        }
        printf("%.2lf\n",ans);
    }
    return 0;
}


 

 

 

Guess you like

Origin blog.csdn.net/u010200793/article/details/11568689