记录编号 123223 评测结果 AAAAAAAAAA
题目名称 [SGU U236]贪心路径 最终得分 100
用户昵称 GravatarHouJikan 是否通过 通过
代码语言 C++ 运行时间 0.008 s
提交时间 2014-09-26 10:13:36 内存使用 0.31 MiB
显示代码纯文本
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <ctime>
#include <iterator>
#include <functional>
#define pritnf printf
#define scafn scanf
#define For(i,j,k) for(int i=(j);i<=(k);(i)++)
using namespace std;
typedef long long LL;
typedef unsigned int Uint; 
const int INF=0x7ffffff;
const double eps=1e-6;
//==============struct declaration==============
struct adj
{
  int to;
  double C,T;
  adj (int to=0,double C=0,double T=0):to(to),C(C),T(T){}
};
//==============var declaration=================
const int MAXN=80;
vector <adj> Edge[MAXN];
int n,m;
double low,high,mid;
//==============function declaration============
bool spfa(double k);
//==============main code=======================
int main()
{  
  string FileName="greedypath";//程序名 
  string FloderName="COGS";//文件夹名 
  freopen((FileName+".in").c_str(),"r",stdin);
  freopen((FileName+".out").c_str(),"w",stdout);
#ifdef DEBUG  
  system(("cp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\standard.cpp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\submit.txt").c_str());
  clock_t Start_Time=clock();
#endif    
  cin>>n>>m;
  For(i,1,m){
    int s,e;double C,T;
    cin>>s>>e>>C>>T;
    Edge[s].push_back(adj(e,C,T));
    //Edge[e].push_back(adj(s,C,T));
  }
  low=0,high=1000;
  while (low+eps<high){
    mid=(low+high)/2.0;
    if (spfa(mid))
      low=mid;
    else
      high=mid;
  }
  if (!spfa((low+high)/2-0.005))
    cout<<"0"<<endl;
  else
    printf("%.2lf",(low+high)/2);
#ifdef DEBUG  
  clock_t End_Time=clock();
  printf("\n\nTime Used: %.4lf Ms\n",double(End_Time-Start_Time)/CLOCKS_PER_SEC);
#endif    
  return 0;
}
//================fuction code====================
bool spfa(double k)
{
  double d[MAXN];
  int out[MAXN];
  bool inq[MAXN];
  memset(inq,0,sizeof(inq));
  memset(out,0,sizeof(out));
  For(i,1,n)
    d[i]=1000000;
  d[1]=0;
  queue <int> Q;
  Q.push(1);
  while (!Q.empty()){
    int now=Q.front();Q.pop();
    inq[now]=false;
    if (++out[now]>n)
      return true;
    int siz=Edge[now].size()-1;
    For(i,0,siz){
      adj &e=Edge[now][i];
      double dist=-e.C+k*e.T;
      if (d[e.to]>d[now]+dist){
        d[e.to]=d[now]+dist;
        if (!inq[e.to])
          Q.push(e.to);
        inq[e.to]=true;
      }
    }
  } 
  return false; 
}