POJ 2387 Til the Cows Come Home(简单的Dijkstra算法)
来源:互联网 发布:手机淘宝海报在线制作 编辑:程序博客网 时间:2024/06/05 00:25
本题是一道简单的模板题目,题目的大意是:给你一个数t,代表有几条已知的道路,再给出一个数n ,代表有n个点,让你求出从1到n的最短距离为多少。(简单的Dijkstra算法)
下面附上本渣渣的AC代码。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>#define inf 0xfffffffusing namespace std;int e[1005][1005],book[1005],dis[1005];int main(){ int t,n; while(cin>>t>>n) { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(i==j) e[i][j]=0; else e[i][j]=inf; } } int t1,t2,t3; for(int i=1;i<=t;i++) { scanf("%d%d%d",&t1,&t2,&t3); e[t1][t2]=e[t2][t1]=min(t3,e[t1][t2]);//可能出现重的路...在此被大坑几发 } for(int i=1;i<=n;i++) { dis[i]=e[1][i]; } memset(book,0,sizeof(book)); book[1]=1; int u; for(int i=1;i<=n-1;i++) { int minx=inf; for(int j=1;j<=n;j++) { if(book[j]==0&&dis[j]<minx) { minx=dis[j]; u=j; } } book[u]=1; for(int v=1;v<=n;v++) { if(e[u][v]<inf) { if(dis[v]>dis[u]+e[u][v]) { dis[v]=dis[u]+e[u][v]; } } } } printf("%d\n",dis[n]); } return 0;}
1 0
- POJ 2387 Til the Cows Come Home(简单的Dijkstra算法)
- POJ 2387 Til the Cows Come Home(Dijkstra算法)
- POJ 2387 Til the Cows Come Home(Dijkstra算法)
- POJ 2387 Til the Cows Come Home(Dijkstra简单题)
- poj 2387 Til the Cows Come Home(dijkstra算法)
- Til the Cows Come Home(poj 2387 Dijkstra算法)
- POJ 2387 Til the Cows Come Home(Dijkstra判重边)
- POJ 2387 Til the Cows Come Home(Dijkstra)
- poj 2387 Til the Cows Come Home(Dijkstra)
- POJ - 2387 Til the Cows Come Home(Dijkstra)
- POJ 2387 Til the Cows Come Home (最短路径,Dijkstra算法)
- poj 2387-Til the Cows Come Home-dijkstra
- POJ 2387 Til the Cows Come Home 最短路-Dijkstra
- POJ 2387 Til the Cows Come Home (Dijkstra)
- Til the Cows Come Home - POJ 2387 Dijkstra
- POJ 2387 || Til the Cows Come Home(最短路,dijkstra
- POJ 2387 Til the Cows Come Home (dijkstra)
- Dijkstra-POJ-2387-Til the Cows Come Home
- Java中重载与重写的区别
- js 回调函数
- android 电容屏(一):电容屏基本原理篇
- PackageManager使用
- POI 操作office2007
- POJ 2387 Til the Cows Come Home(简单的Dijkstra算法)
- cycript脚本例子
- mysql 基本命令
- 3295: [Cqoi2011]动态逆序对 CDQ分治
- Spring的任务调度
- php数组的数据结构
- XMPP之好友获取
- MD5加密
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)