最短路
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 51506 Accepted Submission(s): 22658
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条商店到赛场的路线。
输入保证至少存在1条商店到赛场的路线。
Output
对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间
Sample Input
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
Sample Output
3 2
Source
Recommend
lcy
学习一下最短路吧,Bellman_ford做法
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define Si(a) scanf("%d",&a)
#define Sl(a) scanf("%lld",&a)
#define Sd(a) scanf("%lf",&a)
#define Ss(a) scanf("%s",a)
#define Pi(a) printf("%d\n",(a))
#define Pl(a) printf("%lld\n",(a))
#define Pd(a) printf("%lf\n",(a))
#define Ps(a) printf("%s\n",(a))
#define W(a) while(a--)
#define mem(a,b) memset(a,(b),sizeof(a))
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 10010
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;
struct Edge
{
int u,v;
int cost;
}edge[maxn*2];
int dis[maxn*2],N,M;
int Bellman_Ford()
{
int i,j;
for(i=1;i<=N;i++)
{
if(i==1)dis[i]=0;
else dis[i]=inf;
}
for(i=1;i<=N-1;i++)
{
for(j=1;j<=M*2;j++)
{
if(dis[edge[j].u]+edge[j].cost<dis[edge[j].v])
{
dis[edge[j].v]=dis[edge[j].u]+edge[j].cost;
}
}
}
for(j=1;j<=M*2;j++)
{
if(dis[edge[j].u]+edge[j].cost<dis[edge[j].v])
{
return 0;
}
}
return 1;
}
int main()
{
int i;
while((scanf("%d%d",&N,&M))&&(N!=0||M!=0))
{
mem(edge,0);
for(i=1;i<=M;i++)
{
Si(edge[i].u),Si(edge[i].v),Si(edge[i].cost);
edge[i+M].v=edge[i].u;edge[i+M].u=edge[i].v;
edge[i+M].cost=edge[i].cost;
}
if(Bellman_Ford())
{
Pi(dis[N]);
}
}
return 0;
}
SPFA做法
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define Si(a) scanf("%d",&a)
#define Sl(a) scanf("%lld",&a)
#define Sd(a) scanf("%lf",&a)
#define Ss(a) scanf("%s",a)
#define Pi(a) printf("%d\n",(a))
#define Pl(a) printf("%lld\n",(a))
#define Pd(a) printf("%lf\n",(a))
#define Ps(a) printf("%s\n",(a))
#define W(a) while(a--)
#define mem(a,b) memset(a,(b),sizeof(a))
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 110
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;
int edge[maxn][maxn];
int dis[maxn],N,M;
bool visit[maxn];
int SPFA()
{
int i,j;
for(i=1; i<=N; i++)
{
if(i==1)
{
dis[i]=0;
visit[i]=true;
}
else
{
dis[i]=inf;
visit[i]=false;
}
}
queue<int> Q;
Q.push(1);
while(!Q.empty())
{
int temp=Q.front();
Q.pop();
for(i=1; i<=N; i++)
{
if(dis[temp]+edge[temp][i]<dis[i])
{
dis[i]=dis[temp]+edge[temp][i];
if(!visit[i])
{
Q.push(i);
visit[i]=true;
}
}
}
visit[temp]=false;
}
}
int main()
{
int i,j;
while((scanf("%d%d",&N,&M))&&(N!=0||M!=0))
{
for(i=1; i<=N; i++)
{
for(j=1; j<=i; j++)
{
if(i==j)edge[i][j]=0;
else edge[i][j]=edge[j][i]=inf;
}
}
for(i=1; i<=M; i++)
{
int a,b,c;
Si(a),Si(b),Si(c);
edge[a][b]=c;
edge[b][a]=c;
}
SPFA();
Pi(dis[N]);
}
return 0;
}
Dijkstra做法
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define Si(a) scanf("%d",&a)
#define Sl(a) scanf("%lld",&a)
#define Sd(a) scanf("%lf",&a)
#define Ss(a) scanf("%s",a)
#define Pi(a) printf("%d\n",(a))
#define Pl(a) printf("%lld\n",(a))
#define Pd(a) printf("%lf\n",(a))
#define Ps(a) printf("%s\n",(a))
#define W(a) while(a--)
#define mem(a,b) memset(a,(b),sizeof(a))
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 110
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;
int edge[maxn][maxn];
int dis[maxn],N,M;
bool visit[maxn];
int Dijkstra()
{
int i,j;
for(i=1; i<=N; i++)
{
if(i==1)dis[i]=0;
else dis[i]=inf;
visit[i]=false;
}
int u,v;
while(1)
{
u=-1;
for(v=1;v<=N;v++)
if(!visit[v]&&(u==-1||dis[v]<dis[u]))
u=v;
if(u==-1)break;
visit[u]=true;
for(v=1;v<=N;v++)
{
dis[v]=min(dis[u]+edge[u][v],dis[v]);
}
}
}
int main()
{
int i,j;
while((scanf("%d%d",&N,&M))&&(N!=0||M!=0))
{
for(i=1; i<=N; i++)
{
for(j=1; j<=i; j++)
{
if(i==j)edge[i][j]=0;
else edge[i][j]=edge[j][i]=inf;
}
}
for(i=1; i<=M; i++)
{
int a,b,c;
Si(a),Si(b),Si(c);
edge[a][b]=c;
edge[b][a]=c;
}
Dijkstra();
Pi(dis[N]);
}
return 0;
}
floyd做法
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define Si(a) scanf("%d",&a)
#define Sl(a) scanf("%lld",&a)
#define Sd(a) scanf("%lf",&a)
#define Ss(a) scanf("%s",a)
#define Pi(a) printf("%d\n",(a))
#define Pl(a) printf("%lld\n",(a))
#define Pd(a) printf("%lf\n",(a))
#define Ps(a) printf("%s\n",(a))
#define W(a) while(a--)
#define mem(a,b) memset(a,(b),sizeof(a))
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 110
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;
int edge[maxn][maxn];
int N,M;
int floyd()
{
int i,j,k;
for(k=1; k<=N; k++)
for(i=1; i<=N; i++)
for(j=1; j<=N; j++)
edge[i][j]=min(edge[i][k]+edge[k][j],edge[i][j]);
}
int main()
{
int i,j;
while((scanf("%d%d",&N,&M))&&(N!=0||M!=0))
{
for(i=1; i<=N; i++)
{
for(j=1; j<=i; j++)
{
if(i==j)edge[i][j]=0;
else edge[i][j]=edge[j][i]=inf;
}
}
for(i=1; i<=M; i++)
{
int a,b,c;
Si(a),Si(b),Si(c);
edge[a][b]=c;
edge[b][a]=c;
}
floyd();
Pi(edge[1][N]);
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define Si(a) scanf("%d",&a)
#define Sl(a) scanf("%lld",&a)
#define Sd(a) scanf("%lf",&a)
#define Ss(a) scanf("%s",a)
#define Pi(a) printf("%d\n",(a))
#define Pl(a) printf("%lld\n",(a))
#define Pd(a) printf("%lf\n",(a))
#define Ps(a) printf("%s\n",(a))
#define W(a) while(a--)
#define mem(a,b) memset(a,(b),sizeof(a))
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 110
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;
int dis[maxn],N,M;
bool visit[maxn];
struct node
{
int x,d;
node(int a,int b){x=a;d=b;}
bool operator < (const node & a) const
{
if(d==a.d)return x<a.x;
else return d>a.d;
}
};
vector<node>edge[maxn];
int Dijkstra()
{
int i,j;
for(i=1; i<=N; i++)
{
if(i==1)dis[i]=0;
else dis[i]=inf;
visit[i]=false;
}
priority_queue<node> Q;
Q.push(node(1,dis[1]));
while(!Q.empty())
{
node x=Q.top();
Q.pop();
for(i=0;i<edge[x.x].size();i++)
{
node y=edge[x.x][i];
if(x.d+y.d<dis[y.x])
{
dis[y.x]=x.d+y.d;
Q.push(node(y.x,dis[y.x]));
}
}
}
}
int main()
{
int i,j;
while((scanf("%d%d",&N,&M))&&(N!=0||M!=0))
{
for(i=1;i<=N;i++)edge[i].clear();
for(i=1; i<=M; i++)
{
int a,b,c;
Si(a),Si(b),Si(c);
edge[a].push_back(node(b,c));
edge[b].push_back(node(a,c));
}
Dijkstra();
Pi(dis[N]);
}
return 0;
}