HDU 2680(最短路)(多个起始点)
这道题也是死命TLE。。
http://acm.hdu.edu.cn/showproblem.php?pid=2680
/*
使用pair代替结构
*/ #include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int Ni = ;
const int INF = 0x3f3f3f3f; typedef pair<int,int> pa; int dis[Ni],n;//dis使用1-n的部分 vector<pair<int,int> > eg[Ni]; void Dijkstra(int s)
{
int i,j;
for(i=;i<=n;i++)//要到n
dis[i] = INF;
priority_queue<pa> q; //优先级队列:小顶堆
dis[s] = ;
q.push(make_pair(s,dis[s])); while(!q.empty())
{
pa x = q.top();
q.pop();
int w = x.first;
for(j = ;j<eg[w].size();j++)//遍历x的所有邻接点
{
pa y = eg[w][j];//y是x的邻接点
int u = y.first;
if(dis[u]>x.second+y.second)
{
dis[u] = x.second+y.second;
q.push(make_pair(u,dis[u]));
}
}
} } int main()
{
int m,a,b,y,d,min;//关系个数
while(cin>>n>>m>>y)
{
for(int i = ;i<=n;i++)
eg[i].clear();//初始化
while(m--)
{
cin>>a>>b>>d;
eg[b].push_back(make_pair(a,d));
} Dijkstra(y); int t,x;
min = INF;
cin>>t;
while(t--)
{
cin>>x;
if(dis[x]<min)
min = dis[x];
}
if(min!=INF)
cout<<min<<endl;
else
cout<<"-1\n";
} return ;
}
已ac,514MS
思路1:加辅助点
#include <cstdio>
using namespace std;
const int L = ;
const int INF = <<; int map[L][L];
int vis[L];
int dis[L]; int n,m; void Dijkstra()
{
int i,j,k;
int min;
for(i = ;i<=n;i++)
{
dis[i] = map[][i];
vis[i] = ;
} vis[] = ;
dis[] = ; for(i = ;i<=n;i++)
{
min = INF;
for(j = ;j<=n;j++)
{
if(dis[j]<min && !vis[j])
{
k = j;
min = dis[j];
}
} vis[k] = ; for(j = ;j<=n;j++)
{
if(dis[j] > min+map[k][j] && !vis[j])
{
dis[j] = min+map[k][j];
}
}
}
} void init()
{
int i,j;
for(i = ;i<=n;i++)
{
map[i][i] = ;
for(j = i+;j<=n;j++)
{
map[i][j] = map[j][i] = INF;
}
} while(m--)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
if(w<map[a][b])//可能有同两点,但不同weight
{
map[a][b] = w;
}
}
} int main()
{
int y;
while(~scanf("%d%d%d",&n,&m,&y))
{
int r,min=INF; init(); scanf("%d",&r);
while(r--)
{
int a;
scanf("%d",&a);
map[][a] = ;
} Dijkstra(); if(dis[y]!=INF)
printf("%d\n",dis[y]);
else
printf("-1\n"); } return ;
}
思路2:反向图
#include <cstdio>
using namespace std;
const int L = ;
const int INF = <<; int map[L][L];
int vis[L];
int dis[L]; int n,m; void Dijkstra(int s)
{
int i,j,k;
int min;
for(i = ;i<=n;i++)
{
dis[i] = map[s][i];
vis[i] = ;
} vis[s] = ;
dis[s] = ; for(i = ;i<=n;i++)
{
min = INF;
for(j = ;j<=n;j++)
{
if(dis[j]<min && !vis[j])
{
k = j;
min = dis[j];
}
} vis[k] = ; for(j = ;j<=n;j++)
{
if(dis[j] > min+map[k][j] && !vis[j])
{
dis[j] = min+map[k][j];
}
}
}
} void init()
{
int i,j;
for(i = ;i<=n;i++)
{
map[i][i] = ;
for(j = i+;j<=n;j++)
{
map[i][j] = map[j][i] = INF;
}
} while(m--)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
if(w<map[b][a])//可能有同两点,但不同weight
{
map[b][a] = w;
}
}
} int main()
{
int y;
while(~scanf("%d%d%d",&n,&m,&y))
{
int r,min=INF; init(); Dijkstra(y); scanf("%d",&r);
while(r--)
{
int a;
scanf("%d",&a);
if(dis[a]<min)
min = dis[a];
} if(min!=INF)
printf("%d\n",min);
else
printf("-1\n"); } return ;
}
http://blog.csdn.net/niushuai666/article/details/6794343
这两个思路很值得学习,而且他的两个方法都比我快
HDU 2680(最短路)(多个起始点)的更多相关文章
- HDU - 2680 最短路 spfa 模板
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目大意,就是一个人可以从多个起点开始出发,看到终点的最短路是多少..只有可以运用和hdu2066 ...
- HDU 2680 最短路 迪杰斯特拉算法 添加超级源点
Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu 2680(最短路)
Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 5521 最短路
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU - 2544最短路 (dijkstra算法)
HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...
- hdu 2680 Choose the best route
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...
- hdu 2680 Choose the best route (dijkstra算法)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2680 /************************************************* ...
随机推荐
- canvas图像绘制过程中的注意
特别来记录一下canvas绘制图像,要在图片加载完后,才会将其显示在canvas画布之上,否则会显示不出来:深刻体会,愣是找不到问题... var c=document.getElementById( ...
- ubuntu下apache2使用的简单总结
一. 修改apache2原80端口为90端口 1. 修改/etc/apache2/ports.conf, 将端口80改为90,443,改为444 2. 修改/etc/apache2/sites ...
- [转] Elasticsearch 6.1官方入门教程
一篇比较简要又全面的elasticsearch教程. https://blog.csdn.net/hololens/article/details/78932628
- HTML常用汇总
HTML注释 <!-- --> XHTML:就是要遵守XML规则的HTML标签 DHTML:包含html,js,等动态HTML 表单元素提交时提交的是name属性 get提交.post提交 ...
- L: Long Long Ago---二分
L: Long Long Ago 时间限制: 1 s 内存限制: 128 MB 题目描述 今天SHIELD捕获到一段从敌方基地发出的信息里面包含一串被经过某种算法加密过的的序列 ...
- html-css-js基本理解和简单总结
目录 一.对于网页的基本理解 1.网页是一种数据展示和信息交互的载体 2.网页组成部分 3.支撑一个网页的技术模块 二.html的理解和技术笔记 1.html理解 2.html技术笔记-html标签 ...
- 【chrome】设置chrome允许WebGL从本地载入资源
找到chrome安装路径,然后创建一个快捷方式,右击该快捷方式,在 目标 输入框中加上-allow-file-access-from-files(前面加个空格),通过该快捷方式打开chrome就可以通 ...
- 【Kafka】Producer配置
名称 描述 类型 默认值 bootstrap.servers kafka集群地址,ip+端口,以逗号隔开.不管这边配置的是什么服务器,客户端会使用所有的服务器.配置的列表只会影响初始发现所有主机.配置 ...
- Linux下Tomcat8.0.44配置使用Apr
听说Apr可以提高tomcat很多的性能,配置具体如下 1.安装apr 1.5.2 [root@ecs-3c46 ]# cd /usr/local/src [root@ecs-3c46 src]# w ...
- 事务控制语句,begin,rollback,savepoint,隐式提交的SQL语句
事务控制语句 在MySQL命令行的默认设置下,事务都是自动提交的,即执行SQL语句后就会马上执行COMMIT操作.因此开始一个事务,必须使用BEGIN.START TRANSACTION,或者执行SE ...