题目转载来自此博客 http://my.csdn.net/acdreamers 跟着做嘿嘿
持续更新
1 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1874
思路:dj 是模板题
解题:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
int road[205][205];
int book[205];
int dj[205];
int n,m;
const int inf=0x3f3f3f3f;
int main()
{
int x,y,z,i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
{
if(i==j)
road[i][j]=0;
else
road[i][j]=inf;
}
memset(book,0,sizeof(book));
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
if(z<road[x][y])
{
road[x][y]=z;
road[y][x]=z;
}
}
int begin,end;
scanf("%d%d",&begin,&end);
for(i=0;i<n;i++)
dj[i]=road[begin][i];
book[begin]=1;
int u,minn;
for(i=0;i<n;i++)
{
minn=inf;
for(j=0;j<n;j++)
{
if(!book[j]&&dj[j]<minn)
{
minn=dj[j];
u=j;
}
}
book[u]=1;
for(j=0;j<n;j++)
{
if(dj[j]>dj[u]+road[u][j])
dj[j]=dj[u]+road[u][j];
}
}
if(dj[end]==inf)
printf("-1\n");
else
printf("%d\n",dj[end]);
}
return 0;
}
2 题目:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=508&mosmsg=Submission+received+with+ID+10622709
思路:模板题目主要是题目的理解
解题:Floyed
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 25
#define INF 0xFFFFFFF
int n , cnt;
long long dis[MAXN][MAXN];
/*初始化dis数组*/
void init(){
for(int i = 1 ; i <= 20 ; i++){
for(int j = 1 ; j <= 20 ; j++){
if(i == j)
dis[i][j] = 0;
else
dis[i][j] = INF;
}
}
}
/*求最小值函数*/
long long min(long long a , long long b){
return a < b ? a : b;
}
/*floyd算法*/
void floyd(){
int a , b;
for(int k = 1; k <= 20 ; k++){
for(int i = 1 ; i <= 20 ; i++){
for(int j = 1 ; j <= 20 ; j++)
dis[i][j] = min(dis[i][j] , dis[i][k]+dis[k][j]);
}
}
scanf("%d" , &n);
printf("Test Set #%d\n" , cnt++);
for(int i = 1 ; i <= n ; i++){
scanf("%d%d" , &a , &b);
printf("%2d to %2d: %lld\n" , a , b , dis[a][b]);/*输出问题*/
}
printf("\n");
}
int main(){
int x , a;
cnt = 1;
while(scanf("%d" , &x) != EOF){
init();
for(int i = 1 ; i <= x ; i++){
scanf("%d" , &a);
dis[1][a] = dis[a][1] = 1;
}
for(int i = 2 ; i <= 19 ; i++){
scanf("%d" , &x);
for(int j = 1 ; j <= x ; j++){
scanf("%d" , &a);
dis[i][a] = dis[a][i] = 1;
}
}
floyd();
}
return 0;
}
3:题目:http://acm.hdu.edu.cn/showproblem.php?pid=2544
思路:模板题
解题:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 105
#define inf 0x3f3f3f3f
int road[MAXN][MAXN];
int book[MAXN];
int dj[MAXN];
int n,m;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)break;
memset(dj,0,sizeof(dj));
memset(book,0,sizeof(book));
int i,j;
for(i=1;i<=100;i++)
for(j=1;j<=100;j++)
if(i==j)road[i][j]=0;
else road[i][j]=inf;
int a,b,c;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
road[a][b]=c;
road[b][a]=c;
}
for(i=1;i<=n;i++)
dj[i]=road[1][i];
book[1]=1;
for(i=1;i<=n;i++)
{
int u=-1;
int minn=inf;
for(j=1;j<=n;j++)
{
if(!book[j])
{
if(u==-1||dj[j]<dj[u]){
u=j;
}
}
}
book[u]=1;
for(j=1;j<=n;j++)
{
if(!book[j]&&dj[j]>dj[u]+road[u][j])
dj[j]=dj[u]+road[u][j];
}
}
printf("%d\n",dj[n]);
}
return 0;
}