1072. Gas Station (30)

#include<iostream>
#include<cstring>
#include<iomanip>
#include<algorithm>
#include<vector>
#define INF 0x7ffffff
#define NUM 1000+10+2
using namespace std;
 


int n,m,k,ds;
int map[NUM][NUM]={0};
int visited[NUM];
int dist[NUM];




 struct node{
      int index;   //gas  index
      int mm;     //距离house的最小距离
      int sum;    //所有house的距离和
 node(int index,int mm,int sum):index(index),mm(mm),sum(sum){}   //注意这里的写法,
 ///之前用结构体数组的时候第一个测试点不能通过,可能是因为在存入数组的时候有问题
};
//node gas[11];//结构体数组只能定义在这里,不要写在main函数中,不然会报错
bool cmp(const struct node &a,const struct node& b)
{
       if(a.mm>b.mm)return true;
       else if(a.mm==b.mm)
       {
              if(a.sum<b.sum)return true;
              else if(a.sum==b.sum)return a.index<b.index;
       }


      return false;
}


int strtonum(char *str,int n)
{
        int num;
       if(str[0]=='G')num=atoi(str+1)+n;
       else num=atoi(str);
  return num;
}


/*void init()
{
    int i,j;
    for(i=1;i<NUM;i++)
    {
          
          for(j=1;j<NUM;j++)
          {
                 map[i][j]=0;
          }
    }
}*/


void  dijkstra(int v)
 {
         int i,j,k=0; 
         int min;
        
for(i=1;i<=n+m;i++)
{
visited[i]=0;//注意在这里记得初始化为0
 if(map[v][i]>0&&i!=v)
{
dist[i]=map[v][i];
}
else
{
dist[i]=INF;
}
}
 visited[v]=1;
         for(i=1;i<=n+m;i++)
         {
   
                 min=INF;
                  for(j=1;j<=n+m;j++)
                  {
                          if(min>dist[j]&&v!=j&&!visited[j])
                         {
                                 min=dist[j];
                                  k=j;
                         }
                  }
                  visited[k]=1;
                 //更新
                 for(j=1;j<=n+m;j++)
                 {
                         if(dist[j]>dist[k]+map[k][j]&&map[k][j]>0&&!visited[j])
                         {
                                 dist[j]=dist[k]+map[k][j];
                          }
                 }
                 
         }
  }


int main()
{
       int i,j,x,y;
       int min;
       int sum;


       vector<node>vec;


       char s1[10],s2[10]; 
       int dis; 
       cin>>n>>m>>k>>ds;
       //init();
       for(i=1;i<=k;i++)
       {
             cin>>s1>>s2>>dis;
             x=strtonum(s1,n);
             y=strtonum(s2,n);
             map[x][y]=map[y][x]=dis;
       }
  bool flag;
     // 对于m个源点
  //int count=0;
      for(i=1;i<=m;i++)
      {
 flag=true;
            dijkstra(i+n);
            min=INF;
            sum=0;
            for(j=1;j<=n;j++)
            {
  if(dist[j]>ds)
  {
  flag=false;
  break;
  }
                   if(min>dist[j])min=dist[j];
                   sum+=dist[j];
            }
if(!flag)continue;
            vec.push_back(node(i,min,sum));
            /*gas[i].index=i;
            gas[i].mm=min;
            gas[i].sum=sum;
count++;*/
            //这里如果用结构体数组,那么数组中有的就是空的,排序的时候可能会出现问题,结构体数组怎么初始化是一个问题
//还是用动态数组比较好

      }
 if(vec.size())
    {
           //sort(gas+1,gas+m,cmp);
sort(vec.begin(),vec.end(),cmp);
            cout<<"G"<<vec[0].index<<endl;

double aver=(vec[0].sum*100/100.0)/n;
//cout<<aver<<endl;
double m=vec[0].mm*100/100.0;
            cout<<setiosflags(ios::fixed)<<setprecision(1)<<m<<' ';
cout<<setprecision(1)<<aver<<endl;
    }
    else 
    {
           cout<<"No Solution"<<endl;
}
      
     
     system("pause");
     return 0;
}

















c++解决pipeline system consists of N transfer station, some of which are connected by pipelines. For each of M pipelines the numbers of stations A[i] and B[i], which are connected by this pipeline, and its profitability C[i] are known. A profitability of a pipeline is an amount of dollars, which will be daily yielded in taxes by transferring the gas through this pipeline. Each two stations are connected by not more than one pipeline. The system was built by Soviet engineers, who knew exactly, that the gas was transferred from Ukrainian gas fields to Siberia and not the reverse. That is why the pipelines are unidirectional, i.e. each pipeline allows gas transfer from the station number A[i] to the station number B[i] only. More over, if it is possible to transfer the gas from the station X to the station Y (perhaps, through some intermediate stations), then the reverse transfer from Y to X is impossible. It is known that the gas arrives to the starting station number S and should be dispatched to the buyers on the final station number F. The President ordered the Government to find a route (i.e. a linear sequence of stations which are connected by pipelines) to transfer the gas from the starting to the final station. A profitability of this route should be maximal. A profitability of a route is a total profitability of its pipelines. Unfortunately, the President did not consider that some pipelines ceased to exist long ago, and, as a result, the gas transfer between the starting and the final stations may appear to be impossible... Input The first line contains the integer numbers N (2 ≤ N ≤ 500) and M (0 ≤ M ≤ 124750). Each of the next M lines contains the integer numbers A[i], B[i] (1 ≤ A[i], B[i] ≤ N) and C[i] (1 ≤ C[i] ≤ 10000) for the corresponding pipeline. The last line contains the integer numbers S and F (1 ≤ S, F ≤ N; S ≠ F). Output If the desired route exists, you should output its profitability. Otherwise you should output "No solution".
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值