最短路模板 dij 和 spfa 都可以

spfa:

#include<stdio.h>
#include<string.h>
#include<cstring>
#include<string>
#include<math.h>
#include<queue>
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<cmath> #define INF 0x3f3f3f3f
#define MAX 4*100105 using namespace std; struct node
{
int u,v,len,next;
}Map[MAX]; int n,m,vis[MAX],dist[MAX],k,a[MAX]; void Add(int u,int v,int len)
{
Map[k].u=u;
Map[k].v=v;
Map[k].len=len;
Map[k].next=a[u];
a[u]=k++;
} void Init()
{
k=1; memset(vis,0,sizeof(vis)); for(int i=0;i<MAX;i++)
{
dist[i]=INF;
a[i]=-1;
}
} int spfa()
{
queue<int>Q; int start=1,k; dist[1]=0;
vis[1]=1;
Q.push(start); while(!Q.empty())
{
start=Q.front();
Q.pop(); vis[start]=0;
for(k=a[start];k!=-1;k=Map[k].next)
{
int v=Map[k].v; if(dist[v] > dist[start]+Map[k].len)
{
dist[v]=dist[start]+Map[k].len; if(!vis[v])
{
vis[v]=1;
Q.push(v);
}
}
}
} return 1;
} int main()
{
int i,j,u,v,len; while(scanf("%d%d",&m,&n)!=EOF)
{
Init(); for(i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&len); Add(u,v,len);
Add(v,u,len);
} int ans=spfa(); if(ans)
printf("%d\n",dist[n]);
} return 0;
} dij:
#include<stdio.h>
#include<string.h>
#include<cstring>
#include<string>
#include<math.h>
#include<queue>
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<cmath> #define INF 0x3f3f3f3f
#define MAX 1005 using namespace std; int Map[MAX][MAX],vis[MAX],dist[MAX],n,m; int dij()
{
memset(vis,0,sizeof(vis)); int i,j,k,minn; for(i=2;i<=n;i++)
dist[i]=Map[1][i]; vis[1]=1; for(i=1;i<n;i++)
{
minn=INF;
for(j=1;j<=n;j++)
{
if(!vis[j] && minn > dist[j])
{
k=j;
minn=dist[j];
}
} vis[k]=1; for(j=1;j<=n;j++)
{
if(!vis[j] && dist[k] + Map[k][j] < dist[j])
{
dist[j]=dist[k]+Map[k][j];
}
}
} return dist[n];
} int main()
{
int i,j,u,v,len; while(scanf("%d%d",&m,&n)!=EOF)
{ for(i=0;i<MAX;i++)
for(j=0;j<MAX;j++)
Map[i][j]=INF; for(i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&len); Map[u][v]=Map[v][u]=min(Map[u][v],len);
} int ans=dij(); printf("%d\n",ans);
}
return 0;
}

POJ 2387的更多相关文章

  1. 链式前向星版DIjistra POJ 2387

    链式前向星 在做图论题的时候,偶然碰到了一个数据量很大的题目,用vector的邻接表直接超时,上网查了一下发现这道题数据很大,vector可定会超的,不会指针链表的我找到了链式前向星这个好东西,接下来 ...

  2. hdu 2544 hdu 1874 poj 2387 Dijkstra 模板题

    hdu 2544  求点1到点n的最短路  无向图 Sample Input2 1 //结点数 边数1 2 3 //u v w3 31 2 52 3 53 1 20 0 Sample Output32 ...

  3. 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 ...

  4. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  5. POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

  6. POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)

    题目连接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...

  7. poj 2387 Til the Cows Come Home(dijkstra算法)

    题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ #incl ...

  8. POJ 2387 Til the Cows Come Home(dijkstra裸题)

    题目链接:http://poj.org/problem?id=2387 题目大意:给你t条边(无向图),n个顶点,让你求点1到点n的最短距离. 解题思路:裸的dijsktra,注意判重边. 代码: # ...

  9. POJ 2387 链式前向星下的SPFA

    (POJ)[http://poj.org/problem?id=2387] Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...

  10. Dijkstra单源最短路径,POJ(2387)

    题目链接:http://poj.org/problem?id=2387 Dijkstra算法: //求某一点(源点)到另一点的最短路,算法其实也和源点到所有点的时间复杂度一样,O(n^2); 图G(V ...

随机推荐

  1. POJ 1094 Sorting It All Out(经典拓扑+邻接矩阵)

    ( ̄▽ ̄)" //判环:当入度为0的顶点==0时,则有环(inconsistency) //判序:当入度为0的顶点仅为1时,则能得到有序的拓扑排序,否则无序 //边输入边判断,用contin ...

  2. Java 序列化 对象序列化和反序列化

    Java 序列化 对象序列化和反序列化 @author ixenos 对象序列化是什么 1.对象序列化就是把一个对象的状态转化成一个字节流. 我们可以把这样的字节流存储为一个文件,作为对这个对象的复制 ...

  3. Computation expressions and wrapper types

    原文地址:http://fsharpforfunandprofit.com/posts/computation-expressions-wrapper-types/ 在上一篇中,我们介绍了“maybe ...

  4. Infix to postfix without '(' and ')'

    #include<iostream> #include<stack> #include<string> #include<deque> using na ...

  5. 把对象转换成map

    public static Map toMap(Object object){ Map _result = new CaseInsensitiveMap(); if (object != null) ...

  6. 关于c++的引用

    引用的本质 引用事实上就是两个变量指向同一个地址 int x; int &y = x; cout << &x << endl; cout << &a ...

  7. chapter8_3 c代码和错误

    1.C代码 Lua提供的所有关于动态链接的功能都集中在一个函数中,即package.loadlib. 该函数有两个字符串参数:动态库的完整路径和一个函数名称: local path = "/ ...

  8. Python从json中提取数据

    #json string:s = json.loads('{"name":"test", "type":{"name": ...

  9. js预编译

    先来做三个测试 eg1: var a; a = 1; function a() {}; console.log(a); eg2: var a; function a() {}; console.log ...

  10. for计算100以内的偶数和

    #include "stdio.h" void main() { ,sum=; ;d++) { ==) { sum=sum+d; } }printf("100以内所有偶数 ...