POJ 2387 Til the Cows Come Home(dij+邻接矩阵)
//dijkstra算法;
//这题建邻接矩阵的时候有坑(先读入边后读入点),还有重边;
#include<iostream>
#include<cstdio>
using namespace std;
const int INF=10e7;
const int MAXN=2010;
int k,minn;
int cost[MAXN][MAXN];
int lowcost[MAXN];
bool vis[MAXN]; void dij(int n,int start)
{
for(int i=1;i<=n;i++)
{
lowcost[i]=INF;vis[i]=0;
}
lowcost[start]=0;
for(int i=1;i<=n;i++)
{
k=-1,minn=INF;
for(int i=1;i<=n;i++)
{
if(!vis[i]&&lowcost[i]<minn)
{minn=lowcost[i];k=i;}
}
if(k==-1) break;
vis[k]=1;
for(int i=1;i<=n;i++)
{
if(!vis[i]&&cost[k][i]>=0&&lowcost[k]+cost[k][i]<lowcost[i])
{
lowcost[i]=lowcost[k]+cost[k][i];
}
}
}
} int main()
{
int t,n,a,b,c;
while(scanf("%d%d",&t,&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) cost[i][j]=0;
else cost[i][j]=INF;
}
}
for(int i=1;i<=t;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(c<cost[a][b])
{ cost[a][b]=c; cost[b][a]=c; }
}
dij(n,n);
printf("%d\n",lowcost[1]);
}
return 0;
}
POJ 2387 Til the Cows Come Home(dij+邻接矩阵)的更多相关文章
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- POJ 2387 Til the Cows Come Home (图论,最短路径)
POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...
- POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- POJ 2387 Til the Cows Come Home Dijkstra求最短路径
Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33015 Accepted ...
- POJ 2387 Til the Cows Come Home (最短路 dijkstra)
Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Bessi ...
- POJ 2387 Til the Cows Come Home 【最短路SPFA】
Til the Cows Come Home Description Bessie is out in the field and wants to get back to the barn to g ...
- POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)
原题链接:Til the Cows Come Home 题目大意:有 个点,给出从 点到 点的距离并且 和 是互相可以抵达的,问从 到 的最短距离. 题目分析:这是一道典型的最短路径模版 ...
随机推荐
- PHP Cookei记录用户历史浏览信息的代码
[基础]Cookie常用方法:$_COOKIE['RecordLuHuiDUDU'] 得到Cookiesetcookie('RecordLuHuiDUDU',",time()-3600*24 ...
- 转发一个javascript的编码规范
google出品:http://chajn.org/jsguide/javascriptguide.html
- TCP和UDP报文分片的区别
搞了三年网络,今天才知道这个细节,汗,总结下: MTU大家都知道,是链路层中的网络对数据帧的一个限制,依然以以太网为例,MTU为1500个字节.一个IP数据报在以太网中 传输,如果它的长度大于该MTU ...
- tablespace
CREATE [UNDO] TABLESPACE tablespace_name [DATAFILE datefile_spec1 [,datefile_spec2] ...... [{MININUM ...
- NYIST OJ 题目20 吝啬的王国
DFS水题.题意说明了这是一颗树,那么只要按照根节点DFS下去就好了,DFS的时候记录一下当前在哪个结点,还有父节点是谁,就AC了! #include<cstdio> #include&l ...
- php:跨域
一个没那么难的历史难题,其实只要在被请求端,加一句: header('Access-Control-Allow-Origin: *'); 然后--然后没有了. //跨域访问的时候才会存在此字段 $or ...
- 2016年团体程序设计天梯赛-决赛 L1-6. 一帮一(15)
“一帮一学习小组”是中小学中常见的学习组织方式,老师把学习成绩靠前的学生跟学习成绩靠后的学生排在一组.本题就请你编写程序帮助老师自动完成这个分配工作,即在得到全班学生的排名后,在当前尚未分组的学生中, ...
- Python 逻辑行/物理行
物理行是你在编写程序时所看见的. 逻辑行是Python看见的单个语句.Python假定每个物理行对应一个逻辑行 . 默认地,Python希望每行都只使用一个语句,这样使得代码更加易读. 如果你想要在一 ...
- Java 泛型 泛型代码和虚拟机
Java 泛型 泛型代码和虚拟机 @author ixenos 类型擦除.原始类型.给JVM的指令.桥方法.Java泛型转换的事实 l 类型擦除(type erasure) n Java泛型的处理 ...
- C#实现文件的压缩和解压
这里主要解决文件夹包含文件夹的解压缩问题.1)下载SharpZipLib.dll,在http://www.icsharpcode.net/OpenSource/SharpZipLib/Download ...