2017ecjtu-summer training #4 UESTC 30
最短路
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
在每年的校賽裏,所有進入決賽的同學都會獲得一件很漂亮的T-shirt
。但是每當我們的工作人員把上百件的衣服從商店運回到賽場的時候,卻是非常累的!所以現在他們想要尋找最短的從商店到賽場的路線,你可以幫助他們嗎?
Input
輸入包括多組數據。
每組數據第一行是兩個整數NN,MM(N≤100N≤100,M≤10000M≤10000),NN表示成都的大街上有幾個路口,標號為11的路口是商店所在地,標號為NN的路口是賽場所在地,MM則表示在成都有幾條路。N=M=0N=M=0表示輸入結束。
接下來MM行,每行包括33個整數AA,BB,CC(1≤A1≤A,B≤NB≤N,1≤C≤10001≤C≤1000),表示在路口AA與路口BB之間有一條路,我們的工作人員需要CC分鐘的時間走過這條路。
輸入保證至少存在11條商店到賽場的路線。
Output
對於每組輸入,輸出一行,表示工作人員從商店走到賽場的最短時間。
Sample input and output
Sample Input | Sample Output |
---|---|
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0 |
3 2 |
題意 求最短路徑
用Floyd求最短路徑
AC代碼
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int s[110][110];
int n,m;
void floyd()
{
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(s[i][j]>s[i][k]+s[k][j])
s[i][j]=s[i][k]+s[k][j];
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)
break;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
s[i][j]=inf;
s[i][i]=0;
}
int a,b,c;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
if(s[a][b]>c)
s[a][b]=s[b][a]=c;
}
floyd();
printf("%d\n",s[1][n]);
}
return 0;
}
2017ecjtu-summer training #4 UESTC 30