HDU 2680 Choose the best route多源点最短路
来源:互联网 发布:村上作品推荐 知乎 编辑:程序博客网 时间:2024/05/22 14:58
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=2680
题意
多源点最短路。。。
思路
源点有多个,将所有源点与超级源点连一条距离为0的边,跑最短路算法就行了
#include<cstdio>#include<queue>#include<iostream>#include<vector>#include<map>#include<cstring>#include<string>#include<set>#include<stack>#include<algorithm>#define cle(a) memset(a,0,sizeof(a))#define inf(a) memset(a,0x3f,sizeof(a))#define ll long long#define Rep(i,a,n) for(int i=a;i<=n;i++)using namespace std;#define INF2 9223372036854775807llconst int INF = ( 2e9 ) + 2;const ll maxn = 1010;const int maxm = 2e4+10;struct edge{ int v,w,next;}e[maxm];int head[maxn],d[maxn],vis[maxn];int tot;void addedge(int u,int v,int w){ e[tot].v=v; e[tot].w=w; e[tot].next=head[u]; head[u]=tot++;}int SPFA(int s,int t,int n){ for(int i=0;i<=n;i++) d[i]=INF; d[s]=0; queue<int> q; q.push(s); vis[s]=1; while(!q.empty()) { int u=q.front();q.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].v; int w=e[i].w; if(d[v]>d[u]+w) { d[v]=d[u]+w; if(!vis[v]) { q.push(v); vis[v]=1; } } } } if(d[t]==INF)return -1; else return d[t];}int main(){ int n,m,s; while(~scanf("%d%d%d",&n,&m,&s)) { tot=0; memset(head,-1,sizeof(head)); for(int i=0;i<m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); } int p,temp; scanf("%d",&p); for(int i=0;i<p;i++) { scanf("%d",&temp); addedge(0,temp,0); } int ans=SPFA(0,s,n); printf("%d\n",ans); }}
阅读全文
0 0
- HDU 2680 Choose the best route多源点最短路
- HDU 2680-Choose the best route-最短路+超级源点
- HDU 2680 Choose the best route 多源点+一汇点求最短路
- hdu 2680 Choose the best route 最短路spfa+超级源点
- poj2680---Choose the best route(超级源点+最短路)
- hdu 2680-Choose the best route最短路dijkstra
- HDU 2680 Choose the best route 变形最短路 DIJKSTRA
- HDU 2680Choose the best route(dijk最短路)
- hdu 2680 Choose the best route(最短路)
- hdu 2680 Choose the best route(最短路dijkstra(有重边))
- hdu 2680 Choose the best route 最短路
- HDU 2680 Choose the best route 最短路
- HDU-2680 Choose the best route(最短路[Dijkstra])
- hdu-2680Choose the best route (dijkstra求最短路)
- HDU 2680Choose the best route (最短路+迪杰斯特拉)
- #HDU 2680 Choose the best route 【SPFA最短路】
- hdu 2680 Choose the best route(最短路)
- HDU 2680 Choose the best route(最短路,反向)
- 各大IT公司校园招聘程序猿笔试、面试题集锦
- 计算机网络之读Internet网发展史 读后感
- 自定义控件之侧滑面板
- 函数指针,才是一切一切的根基
- 解决PHP5.6版本“No input file specified”的问题
- HDU 2680 Choose the best route多源点最短路
- MyBatis映射错误,No qualifying bean of type 'xx.xx.UserDao' available
- 微笑面对每一天
- 仿百度页面(html+css)
- Intel82599网卡TCP/IP与FSDK对比测试
- sublime插件
- 【JZOJ5347】遥远的金字塔
- 使用JAVA读写Properties属性文件
- WCF透明代理类,动态调用,支持async/await