最短路
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 42674 Accepted Submission(s): 18691
Problem Description
在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?
Input
输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
输入保证至少存在1条商店到赛场的路线。
Output
对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间
Sample Input
2 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0
Sample Output
Source
UESTC 6th Programming Contest Online
这个是以前做的,注释比较全。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 0x3f3f3f
int min(int x,int y)//求出x,y中较小的那个
{
if(x >= y)
return y;
return x;
}
int time[111][111];//构建图
int dis[111],vis[111];//d数组 求距离,vis数组 做标记
int n,m;
void dijkstra(int s)
{
int i,j,k;
memset(vis, 0, sizeof(vis));//一开始没有图中无点,全记作 0
for( i = 1; i <= n; i++)
dis[i] = MAX;//假定所有距离为 MAX(不是无穷大,但也很大^_^ )
dis[s] = 0;// 点 1 在图中,dis记作 0
while(1)
{
k = -1;// k为 vis的下标
for( i = 1; i <= n; i++)
if(!vis[i] && ( k == -1 || dis[i] < dis[k] ))
k = i;
if( k == -1) break;//退出循环
vis[k] = 1;// 说明点k跟点1已连接,标记为1
for(i = 1; i <= n; i++)
dis[i] = min(dis[i],dis[k]+time[i][k]);//判断看是否将边松弛,dis总是为最短的那条边
}
printf("%d\n",dis[n]);//n为终点,故输出 dis[n].
}
int main()
{
int i, j, k;
while(scanf("%d%d",&n, &m)!=EOF,n|m)
{
int a,b,c;
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
time[i][j] = MAX;//先把两点间距离设为MAX
while(m--)
{
scanf("%d%d%d",&a, &b, &c);
if(a!=b) //最好加上
time[a][b] = time[b][a] = c;//图上不同两点a,b及之间的距离c
}
dijkstra(1);//题目求从 1 到 n的最短路,故从1开始而不是0开始
}
return 0;
}
========================================================================
2016.2.1重新三种方法走起
dijkstra :
#include<cstdio>
#include<cstring>
#include<cmath>
#define Wi(a) while(a--)
#define Si(a) scanf("%d", &a)
#define Pi(a) printf("%d\n", (a))
#define mem(a, b) memset(a, (b), sizeof(a))
#define INF 0x3f3f3f
#include<algorithm>
using namespace std;
const int mx = 300;
int n, m;
int map[mx][mx];
int dis[mx], vis[mx];
void dijkstra(int s)
{
mem(vis, 0);
int i, j, k;
for(i = 1; i <= n; i++)
dis[i] = INF;
dis[s] = 0;
while(1){
k = -1;
for(i = 1; i <= n; i++)
{
if((k==-1 || dis[k] > dis[i]) && !vis[i]) k = i;
}
if(k==-1) break;
vis[k] = 1;
for(i = 1; i <= n; i++)
{
dis[i] = min(dis[i], dis[k]+map[i][k]);
}
}
Pi(dis[n]);
}
int main(){
while(scanf("%d%d", &n, &m)==2, n+m)
{
int i, j, k;
int a, b, c;
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
map[i][j] = ( i==j ? 0 : INF);
}
}
Wi(m)
{
scanf("%d%d%d", &a, &b, &c);
if(map[a][b] > c)
map[a][b] = map[b][a] = c;
}
dijkstra(1);
}
return 0;
}
floyd:
#include<cstdio>
#include<cstring>
#include<cmath>
#define Wi(a) while(a--)
#define Si(a) scanf("%d", &a)
#define Pi(a) printf("%d\n", (a))
#define mem(a, b) memset(a, (b), sizeof(a))
#define INF 0x3f3f3f
#include<algorithm>
using namespace std;
const int mx = 300;
int n, m;
int map[mx][mx];
void floyd()
{
int i, j, k;
for(k = 1; k <= n; k++)
{
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
map[i][j] = min(map[i][j], map[i][k]+map[k][j]);
}
}
}
Pi(map[1][n]);
}
int main(){
while(scanf("%d%d", &n, &m)==2, n+m)
{
int i, j, k;
int a, b, c;
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
map[i][j] = ( i==j ? 0 : INF);
}
}
Wi(m)
{
scanf("%d%d%d", &a, &b, &c);
if(map[a][b] > c)
map[a][b] = map[b][a] = c;
}
floyd();
}
return 0;
}
SPFA:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#define Wi(a) while(a--)
#define Si(a) scanf("%d", &a)
#define Pi(a) printf("%d\n", (a))
#define mem(a, b) memset(a, (b), sizeof(a))
#define INF 0x3f3f3f
#include<algorithm>
using namespace std;
const int mx = 100+10;
const int mr = 10000+10;
int n, m;
struct node{
int from, to, val, next;
};
node edge[mr];
int head[mx], edgenum;
void addedge(int u, int v, int w)
{
node E = {u, v, w, head[u]};
edge[edgenum] = E;//22和23这两行 相当于下面四行~
// edge[edgenum].from = u;
// edge[edgenum].to = v;
// edge[edgenum].val = w;
// edge[edgenum].next = head[u];
head[u] = edgenum++;
}
void init()
{
edgenum = 0;
mem(head, -1);
}
int dis[mx], vis[mx], used[mx];
void SPFA(int s)
{
queue<int> q;
mem(dis, INF); mem(vis, 0); mem(used, 0);
q.push(s);
dis[s] = 0;
vis[s] = 1;
used[1]++;
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = 0;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(dis[v] > dis[u]+edge[i].val)
{
dis[v] = dis[u] + edge[i].val;
if(!vis[v])
{
vis[v] = 1;
q.push(v);
used[v]++;
if(used[v] > n) return;
}
}
}
}
Pi(dis[n]);
}
void getmap()
{
int a, b, c;
Wi(m){
scanf("%d%d%d", &a, &b, &c);
addedge(a, b, c);
addedge(b, a, c);
}
}
int main()
{
while(scanf("%d%d", &n, &m)==2, n+m)
{
init();
getmap();
SPFA(1);
}
return 0;
}