加了一些花的最短路,点的个数为500不需要堆优化,改一下dij的判断条件就可以了。

上代码:

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=;
const int inf=0x3f3f3f3f; struct node
{
int id;
int cost;
int time;
}; int s,e;
int n,m;
vector<int> line0;
vector<int> line1;
vector<node> edge[maxn];
int d0[maxn];
int d1[maxn]; void build_map()
{
int v1,v2,ow,cost,time;
scanf("%d %d %d %d %d",&v1,&v2,&ow,&cost,&time);
node temp;
temp.cost=cost;
temp.time=time;
temp.id=v2;
edge[v1].push_back(temp);
if(ow==)
{
temp.id=v1;
edge[v2].push_back(temp);
}
} void dij0() // deug
{
int vis[maxn];
int pre[maxn];
int cost[maxn];
cost[s]=;
pre[s]=-;
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++) d0[i]=inf;
d0[s]=;
while()
{
int v=-;
for(int i=;i<=n;i++)
{
if(vis[i]== && ( v==- || d0[v] > d0[i])) v=i;
}
if(v==-) break;
vis[v]=;
int len=edge[v].size();
for(int i=;i<len;i++)
{
node temp=edge[v][i];
if(vis[temp.id] == )
{
if(d0[temp.id] > d0[v]+temp.time )
{
d0[temp.id]=d0[v]+temp.time; //
pre[temp.id]=v;
cost[temp.id]=cost[v]+temp.cost;
}
else if(d0[temp.id] == d0[v]+temp.time)
{
if(cost[temp.id] > cost[v]+temp.cost)
{
cost[temp.id]=cost[v]+temp.cost;
pre[temp.id]=v;
}
} }
}
}
// cout<<"1"<<endl;
int zz=e;
line0.push_back(e);
while(pre[zz]!=-) //
{
line0.push_back(pre[zz]);
zz=pre[zz];
// cout<<zz<<endl;
}
//cout<<"2"<<endl; reverse(line0.begin(),line0.end());
} void dij1() // deug
{
int vis[maxn];
int pre[maxn];
int cost[maxn];
cost[s]=;
pre[s]=-;
memset(vis,,sizeof(vis));
// vis[s]=1;
for(int i=;i<=n;i++) d1[i]=inf;
d1[s]=;
while()
{
int v=-;
for(int i=;i<=n;i++)
{
if(vis[i]== && (v==- || d1[v] > d1[i])) v=i;
}
if(v==-) break;
vis[v]=;
// line1.push_back(v);
int len=edge[v].size();
for(int i=;i<len;i++)
{
node temp=edge[v][i];
if(vis[temp.id]==)
{
if(d1[temp.id] > d1[v]+temp.cost)
{
d1[temp.id]=d1[v]+temp.cost; //
pre[temp.id]=v;
cost[temp.id]=cost[v]+;
}
else if(d1[temp.id] == d1[v]+temp.cost)
{
if(cost[temp.id] > cost[v]+)
{
cost[temp.id]=cost[v]+;
pre[temp.id]=v;
}
}
} }
} int zz=e; // route
line1.push_back(e);
while(pre[zz]!=-)
{
line1.push_back(pre[zz]);
zz=pre[zz];
} reverse(line1.begin(),line1.end());
}
int check()
{
int len1=line1.size();
int len0=line0.size();
if(len1 != len0) return ;
for(int i=;i<len1;i++)
{
if(line0[i] != line1[i]) return ;
}
return -;
} int main()
{
cin>>n>>m;
while(m--) build_map();
cin>>s>>e;
dij0();// 0 refers to the shortest time
dij1();// 1 refers to the shortset cost
if(check() == ) // diff
{
printf("Time = %d: ",d0[e]);
cout<<line0[];
for(int i=;i<line0.size();i++) cout<<" => "<<line0[i];
cout<<endl; printf("Distance = %d: ",d1[e]);
cout<<line1[];
for(int i=;i<line1.size();i++) cout<<" => "<<line1[i];
cout<<endl;
}
else
{
printf("Time = %d; ",d0[e]);
printf("Distance = %d: ",d1[e]);
cout<<line1[];
for(int i=;i<line1.size();i++) cout<<" => "<<line1[i];
cout<<endl;
}
return ;
}

最新文章

  1. js 获取据当前时间n天前的时间
  2. SoapUI调用webservice实现的两种方式
  3. 解决ftp连接出现 无法从控制 Socket 读取。Socket 错误 = #10054。
  4. js 实现文字列表滚动效果
  5. [android警告] AndroidManifest.xml警告 Should explicitly set android:allowBackup to true or false
  6. OC之protocol监听器的实现
  7. Cache基础知识OR1200在ICache一个简短的引论
  8. Partial Tree
  9. PAT (Advanced Level) 1104. Sum of Number Segments (20)
  10. Ajax常用实例
  11. C# Linq GroupBy 分组过滤求和
  12. linux命令df中df -h和df -i
  13. 不同版本的IDE ,对应的选项 有变化
  14. 「LuoguP1280」尼克的任务
  15. Linux 磁盘告警分析
  16. VMware 获取该虚拟机的所有权失败
  17. win7怎么打开加锁文件夹
  18. N76E003 工程创建教程
  19. (转)Hashtable与ConcurrentHashMap区别
  20. php session_start()

热门文章

  1. K8S集群Master高可用实践
  2. 蓝牙BLE: GATT Profile 简介(GATT 与 GAP)
  3. Docker 容器日志分析
  4. 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters
  5. python获取风和天气城市数据 ID
  6. 算法习题---5-5复合词(UVa10391)
  7. Qt编写气体安全管理系统6-地图监控
  8. Pycharm一些额外使用笔记
  9. 【物联网】arduino wifi
  10. 安装私有docker仓库