【LDU】2017级课后练习题#2

A - 变形课 

http://acm.hdu.edu.cn/showproblem.php?pid=1181

Problem Description
呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规律:如果咒语是以a开头b结尾的一个单词,那么它的作用就恰好是使A物体变成B物体. 
Harry已经将他所会的所有咒语都列成了一个表,他想让你帮忙计算一下他是否能完成老师的作业,将一个B(ball)变成一个M(Mouse),你知道,如果他自己不能完成的话,他就只好向Hermione请教,并且被迫听一大堆好好学习的道理.
 

Input
测试数据有多组。每组有多行,每行一个单词,仅包括小写字母,是Harry所会的所有咒语.数字0表示一组输入结束.
 

Output
如果Harry可以完成他的作业,就输出"Yes.",否则就输出"No."(不要忽略了句号)
 

Sample Input
  
  
so soon river goes them got moon begin big 0
 

Sample Output
  
  
Yes.
Hint
Hint
Harry 可以念这个咒语:"big-got-them".
 

#include<stdio.h>
#include<string.h>
#define N 5000
#include<algorithm>
using namespace std;
char ch[N][N];
int use[N];
int F,n;
void dfs(int x)
{
     int j;
     int l=strlen(ch[x]);
     if(ch[x][l-1]=='m')
     {
         F=1;
         return ;
     }
     for(j=0;j<n;j++)
     {
         if(ch[x][l-1]==ch[j][0]&&use[j]==0)
         {
             use[j]=1;
             dfs(j);
             use[j]=0;
         }
     }
}
int main()
{

    while(~scanf("%s",ch[0]))
    {
        int i;
        int flag=0;
        F=0;n=1;
        memset(use,0,sizeof(use));
        while(1)
        {
            scanf("%s",ch[n]);
            int l=strlen(ch[n]);
            if(ch[n][l-1]=='m')
               flag=1;
            if(ch[n][0]=='0')
                break;

            n++;
        }
              if(!flag)
                  printf("No.\n");
              else
               {
                  for(i=0;i<n;i++)
                  {
                    if(ch[i][0]=='b')
                        dfs(i);
                  }
                  if(F)
                      printf("Yes.\n");
                  else
                      printf("No.\n");

               }

       }return 0;


}

B - Sticks 

http://acm.hdu.edu.cn/showproblem.php?pid=1455


Problem Description
George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero. 
 

Input
The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there are at most 64 sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero.
 

Output
The output file contains the smallest possible length of original sticks, one per line. 
 

Sample Input
  
  
9 5 2 1 5 2 1 5 2 1 4 1 2 3 4 0
 

Sample Output
  
  
6 5

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#define N 100
using namespace std;
int vis[N],a[N],n;
int cmp(const void *a,const void *b)
{
    return  *(int *)b-*(int *)a;
}
int dfs(int len,int remains_len,int num)
{
      int i;
      if(remains_len==0&&num==0)
          {  return len;}
      if(remains_len==0)
          {  remains_len=len;}
      for(i=0;i<n;i++)
      {
              if(vis[i]==1)
                 continue;
              if(remains_len>=a[i])
              {
                    vis[i]=1;
                    if(dfs(len,remains_len-a[i],num-1))
                        return len;
                    vis[i]=0;
              if(a[i]==remains_len||len==remains_len)
                 break;
              while(a[i]==a[i+1])
                  i++;
             }

          }
      return 0;

}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
                    int sum=0;
                    int len,k;
                    for(int i=0;i<n;i++)
                    {
                            scanf("%d",&a[i]);
                            sum+=a[i];
                    }
                    qsort(a,n,sizeof(int),cmp);
                    for(len=a[0];len<=sum;len++)
                    {
                        memset(vis,0,sizeof(vis));
                        if(sum%len==0)
                        {
                                     k=dfs(len,0,n);
                                      if(k)
                                        break;
                        }
                    }
                    printf("%d\n",k);
    }
    return 0;
}

C - Red and Black 

http://acm.hdu.edu.cn/showproblem.php?pid=1312

Problem Description
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above. 
 

Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

'.' - a black tile 
'#' - a red tile 
'@' - a man on a black tile(appears exactly once in a data set) 
 

Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself). 
 

Sample Input
  
  
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
 

Sample Output
  
  
45 59 6 13
 
#include<stdio.h>
#include<string.h>
int a[21][21];
int book[21][21],n,m,sum;
void dfs(int x,int y)
{
     int next[4][2]={ {0,1},{1,0},{0,-1},{-1,0}};
     int k,tx,ty;
     for(k=0;k<=3;k++)
     {
         tx=x+next[k][0];
         ty=y+next[k][1];
         if(tx<0||tx>n||ty<0||ty>m)
            continue;
         if(a[tx][ty]>0&&book[tx][ty]==0)
         {
              sum++;
              book[tx][ty]=1;
              dfs(tx,ty);
         }
     }
     return ;
}
int main()
{
    int i,j,startx,starty;
    char str[21][21];
    while(scanf("%d%d",&m,&n),(n||m))
   {
        memset(str,'\0',sizeof(str));
        memset(book,0,sizeof(book));
        memset(a,-1,sizeof(a));
        for(i=0; i<n; i++)
        {
            scanf("%s",str[i]);
        }
        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                if(str[i][j]=='@')
                {
                    startx=i;
                    starty=j;
                    a[i][j]=1;
                }
                if(str[i][j]=='#')
                {
                    a[i][j]=0;
                }
                if(str[i][j]=='.')
                {
                    a[i][j]=1;
                }
            }
        }
        book[startx][starty]=1;
        sum=1;
        dfs(startx,starty);
        printf("%d\n",sum);

    }
    return 0;
}

D - Square 

http://acm.hdu.edu.cn/showproblem.php?pid=1518

Problem Description
Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square?
 

Input
The first line of input contains N, the number of test cases. Each test case begins with an integer 4 <= M <= 20, the number of sticks. M integers follow; each gives the length of a stick - an integer between 1 and 10,000.
 

Output
For each case, output a line containing "yes" if is is possible to form a square; otherwise output "no".
 

Sample Input
  
  
3 4 1 1 1 1 5 10 20 30 40 50 8 1 7 2 6 4 4 3 5
 

Sample Output
  
  
yes no yes
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=100;
int n,m,len[maxn],sum;
int vis[maxn],flag;
void dfs(int now,int length,int pos)
{
    if(now==5)
    {
        flag=1;
        return ;
    }
    if(length==sum/4)
    {
        dfs(now+1,0,0);
        if(flag)
          return ;
    }
    for(int i=pos;i<m;i++)
    {
        if(length+len[i]>sum)
        {
           return ;
        }
        if(!vis[i]&&length+len[i]<=sum/4)
        {
           vis[i]=1;
           dfs(now,length+len[i],i+1);
           if(flag)
              return ;
           vis[i]=0;
        }
    }
}
int main()
{
     scanf("%d",&n);
     while(n--)
     {
         sum=0;
         scanf("%d",&m);
         for(int i=0;i<m;i++)
         {
            scanf("%d",&len[i]);
            sum+=len[i];
         }
         sort(len,len+m);
         if((sum%4!=0)||(len[m-1]>sum/4))
         {
             printf("no\n");
         }
         else
         {
             memset(vis,0,sizeof(vis));
             flag=0;
             dfs(1,0,0);
             if(flag)
                 printf("yes\n");
             else
                 printf("no\n");
         }
    }return 0;

}

F - Prime Ring Problem 

http://acm.hdu.edu.cn/showproblem.php?pid=1016

Problem Description
A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.

Note: the number of first circle should always be 1.


 

Input
n (0 < n < 20).
 

Output
The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.

You are to write a program that completes above process.

Print a blank line after each case.
 

Sample Input
  
  
6 8
 

Sample Output
  
  
Case 1: 1 4 3 2 5 6 1 6 5 2 3 4 Case 2: 1 2 3 8 5 6 7 4 1 2 5 8 3 4 7 6 1 4 7 6 5 8 3 2 1 6 7 4 3 8 5 2
#include<stdio.h>
#include<string.h>
int a[50],x[50],n,book[50];
void solve()
{	int i,k=0;	
    for(i=2;i<50;i++)
    {
	if(x[i]==0)
	 {	
	 int j;
	  for(j=2*i;j<50;j+=i)
      {x[j]=2;}
	   x[i]=1; 
	  } 
     }
}
void dfs(int t)
{
    int i;
	if(t==n+1&&x[a[1]+a[n]]==1)
	{
		for(i=1;i<n;i++)
		{
			printf("%d ",a[i]);
		}printf("%d\n",a[i]);
		return ;
	}
	for(i=2;i<=n;i++)
	{
	   if(!book[i]&&x[a[t-1]+i]==1)
	   {
   	      a[t]=i;
		  book[i]=1;
		  dfs(t+1);
		  book[i]=0;	
   	   }
	}
	
}
int main()
{
	int k=1;
	a[1]=1;
	solve();
	while(~scanf("%d",&n))
	{
		memset(book,0,sizeof(book));
		printf("Case %d:\n",k++);
		dfs(2);
		printf("\n");
		
	}return 0;
	
}

G - 全排列

https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1384

给出一个字符串S(可能有重复的字符),按照字典序从小到大,输出S包括的字符组成的所有排列。例如:S = "1312",
输出为:

1123
1132
1213
1231
1312
1321
2113
2131
2311
3112
3121
3211
Input
输入一个字符串S(S的长度 <= 9,且只包括0 - 9的阿拉伯数字)
Output
输出S所包含的字符组成的所有排列
Input示例
1312
Output示例
1123
1132
1213
1231
1312
1321
2113
2131
2311
3112
3121
3211

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int vis[15],a[15],b[15];
char c[15];
int len;
void dfs(int t)
{
    int i;
    if(t==len)
    {
        for(i=0; i<len; i++)
        {
            printf("%d",b[i]);
        }
        printf("\n");
        return ;
    }
    else
    {
        for(i=0; i<len; i++)
        {
            if(!vis[i])
            {
                vis[i]=1;
                b[t]=a[i];
                dfs(t+1);
                vis[i]=0;
                while(i-1<len&&a[i+1]==a[i])i++;
            }

        }

    }

}
int main()
{
    int i;
    scanf("%s",c);
    len=strlen(c);
    for(i=0; i<len; i++)
    {
        a[i]=c[i]-'0';
    }
    sort(a,a+len);
    memset(vis,0,sizeof(vis));
    dfs(0);
    return 0;

}


H - 畅通工程 

http://acm.hdu.edu.cn/showproblem.php?pid=1863

Problem Description
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。
 

Input
测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N 
行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。
 

Output
对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。
 

Sample Input
  
  
3 3 1 2 1 1 3 2 2 3 4 1 3 2 3 2 0 100
 

Sample Output
  
  
3 ?
 
#include<bits/stdc++.h>
using namespace std;
int n,m;
const int inf=0x3f3f3f3f;
int G[105][105],vis[105],dis[105];
void prim()
{
     int i,j,k,minz,ans=0;
     for(i=1;i<=m;i++)
     {
         dis[i]=G[1][i];
     }
     vis[1]=1;
     for(i=2;i<=m;i++)
     {
          minz=inf;k=-1;
          for(j=1;j<=m;j++)
         {
              if(!vis[j]&&minz>dis[j])
              {
                   minz=dis[j];
                   k=j;
              }
         }
         if(k==-1)
          {
             printf("?\n");
             return ;
          }
         vis[k]=1;
         ans+=minz;
         for(j=1;j<=m;j++)
         {
             if(!vis[j]&&dis[j]>G[k][j])
             {
                  dis[j]=G[k][j];
             }
         }
     }
     printf("%d\n",ans);
     return ;
}
int main()
{

      while(~scanf("%d%d",&n,&m),n)
      {
            memset(vis,0,sizeof(vis));
            memset(G,inf,sizeof(G));
           for(int i=0;i<n;i++)
           {
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                if(G[a][b]>c)
                    G[a][b]=G[b][a]=c;
           }
           prim();
      }
      return 0;
}


I - Highways 

http://poj.org/problem?id=2485

Description

The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system. 

Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways. 

The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.

Input

The first line of input is an integer T, which tells how many test cases followed. 
The first line of each case is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. There is an empty line after each test case.

Output

For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.

Sample Input

1

3
0 990 692
990 0 179
692 179 0

Sample Output

692

Hint

Huge input,scanf is recommended.


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
int n;
int vis[550],dis[550],G[550][550];
void prim()
{
     int i,j,k,minz,ans=-1;
     for(i=1;i<=n;i++)
     {
         dis[i]=G[1][i];
     }
     vis[1]=1;
     for(i=2;i<=n;i++)
     {
          k=-1;minz=inf;
          for(j=1;j<=n;j++)
          {
                if(!vis[j]&&minz>dis[j])
                {
                    minz=dis[j];
                    k=j;
                }
          }
          vis[k]=1;
          ans=max(ans,minz);
          for(j=1;j<=n;j++)
          {
                if(!vis[j]&&dis[j]>G[k][j])
                {
                     dis[j]=G[k][j];
                }
          }
     }
     printf("%d\n",ans);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
          int i,j;
          memset(vis,0,sizeof(vis));
          scanf("%d",&n);
          for(i=1;i<=n;i++)
          {
               for(j=1;j<=n;j++)
               {
                   scanf("%d",&G[i][j]);
               }
          }
          prim();
    }
    return 0;
}
J - Agri-Net 

http://poj.org/problem?id=1258

Description

Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. 
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms. 
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm. 
The distance between any two farms will not exceed 100,000. 

Input

The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

Output

For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

Sample Input

4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0

Sample Output

28
#include<stdio.h>
#include<algorithm>
#include<string.h>
int n;
const int inf=0x3f3f3f3f;
int G[105][105],vis[105],dis[105];
void prim()
{
      int i,j,k,minz,ans=0;
      for(i=1;i<=n;i++)
      {
          dis[i]=G[1][i];
      }
      vis[1]=1;
      for(i=2;i<=n;i++)
      {
          k=-1,minz=inf;
          for(j=1;j<=n;j++)
          {
              if(!vis[j]&&dis[j]<minz)
              {
                  minz=dis[j];
                  k=j;
              }
          }
          vis[k]=1;
          ans+=minz;
          for(j=1;j<=n;j++)
          {
              if(!vis[j]&&G[k][j]<dis[j])
              {
                  dis[j]=G[k][j];
              }
          }
      }
      printf("%d\n",ans);
      return ;
}
int main()
{
      while(~scanf("%d",&n))
      {
          memset(vis,0,sizeof(vis));
          for(int i=1;i<=n;i++)
          {
              for(int j=1;j<=n;j++)
              {
                  scanf("%d",&G[i][j]);
              }
          }
          prim();
      }
      return 0;

}
K - Truck History 

http://poj.org/problem?id=1789

Description

Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company has its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (each letter on each position has a very special meaning but that is unimportant for this task). At the beginning of company's history, just a single truck type was used but later other types were derived from it, then from the new types another types were derived, and so on. 

Today, ACM is rich enough to pay historians to study its history. One thing historians tried to find out is so called derivation plan -- i.e. how the truck types were derived. They defined the distance of truck types as the number of positions with different letters in truck type codes. They also assumed that each truck type was derived from exactly one other truck type (except for the first truck type which was not derived from any other type). The quality of a derivation plan was then defined as 
1/Σ(to,td)d(to,td)

where the sum goes over all pairs of types in the derivation plan such that t o is the original type and t d the type derived from it and d(t o,t d) is the distance of the types. 
Since historians failed, you are to write a program to help them. Given the codes of truck types, your program should find the highest possible quality of a derivation plan. 

Input

The input consists of several test cases. Each test case begins with a line containing the number of truck types, N, 2 <= N <= 2 000. Each of the following N lines of input contains one truck type code (a string of seven lowercase letters). You may assume that the codes uniquely describe the trucks, i.e., no two of these N lines are the same. The input is terminated with zero at the place of number of truck types.

Output

For each test case, your program should output the text "The highest possible quality is 1/Q.", where 1/Q is the quality of the best derivation plan.

Sample Input

4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0

Sample Output

The highest possible quality is 1/3.

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
int n,dis[2010],vis[2010],G[2010][2010];
char str[2010][10];
int find(int a,int b)
{
       int i;
       int count=0;
       for(i=0;i<10;i++)
       {
           if(str[a][i]!=str[b][i])
           {
               count++;
           }
       }
       return count;
}
void prim()
{
    int i,j,k,minz,ans=0;
    for(i=0;i<n;i++)
    {
         dis[i]=G[0][i];
    }
    vis[0]=1;
    for(i=1;i<n;i++)
    {
         k=-1,minz=inf;
         for(j=0;j<n;j++)
         {
              if(!vis[j]&&minz>dis[j])
              {
                   minz=dis[j];
                   k=j;
              }
         }
         if(k==-1)
         {
              break;
         }
         ans+=minz;
         vis[k]=1;
         for(j=0;j<n;j++)
         {
               if(!vis[j]&&G[k][j]<dis[j])
               {
                     dis[j]=G[k][j];
               }
         }
    }
    printf("The highest possible quality is 1/%d.\n",ans);
}
int main()
{
      while(~scanf("%d",&n),n)
      {
            int i,j;
            memset(vis,0,sizeof(vis));
            for(i=0;i<n;i++)
            {
                 scanf("%s",str[i]);
            }
            for(i=0;i<n;i++)
            {
                for(j=0;j<i;j++)
                {
                    G[i][j]=G[j][i]=find(i,j);
                }
            }
            prim();
      }
      return 0;
}

L - 畅通工程续 

http://acm.hdu.edu.cn/showproblem.php?pid=1874

Problem Description
某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。

现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
 

Input
本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。
 

Output
对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.
 

Sample Input
  
  
3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2
 

Sample Output
  
  
2 -1

#include<stdio.h>
const int inf=0x3f3f3f3f;
int n,m,G[200][200];
int main()
{
      while(~scanf("%d%d",&n,&m))
      {
          for(int i=0;i<n;i++)
          {
              for(int j=0;j<n;j++)
              {
                  if(i==j)
                  {
                      G[i][j]=0;
                  }
                  else
                  {
                      G[i][j]=inf;
                  }
              }
          }
          for(int i=0;i<m;i++)
          {
              int a,b,c;
              scanf("%d%d%d",&a,&b,&c);
              if(G[a][b]>c)
              {
                  G[a][b]=G[b][a]=c;
              }
          }
          int s,f;
          scanf("%d%d",&s,&f);
          for(int k=0;k<n;k++)
          {
               for(int i=0;i<n;i++)
               {
                   for(int j=0;j<n;j++)
                   {
                         if(G[i][j]>G[i][k]+G[k][j])
                         {
                             G[i][j]=G[i][k]+G[k][j];
                         }
                   }
               }
          }
          if(G[s][f]==inf)
          {
              printf("-1\n");
          }
          else
          printf("%d\n",G[s][f]);
      }
      return 0;
}

M - 最短路 

http://acm.hdu.edu.cn/showproblem.php?pid=2544

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
  
  
3 2
 
#include<cstdio>
#include<math.h>
#include<memory.h>
using namespace std;
#define inf 0x3f3f3f3f
int map[105][105],dis[105],vis[105],n,m;
void dij(int a,int b)
{
    int i,j,minz,k;
    for(i=1;i<=n;i++)
    {
         dis[i]=map[1][i];
         vis[i]=0;
    }
    vis[a]=1;
    for(i=1;i<=n;i++)
    {
         minz=inf;
         for(j=1;j<=n;j++)
         {
             if(vis[j]==0&&dis[j]<minz)
             {
                  k=j;
                  minz=dis[j];
             }
         }
         vis[k]=1;
         for(j=1;j<=n;j++)
         {
            if(vis[j]==0&&dis[j]>map[k][j]+dis[k])
            {
                dis[j]=dis[k]+map[k][j];
            }

         }
    }
    printf("%d\n",dis[b]);
}
int main()
{
      int i,j,k,a,b,c;
      while(~scanf("%d%d",&n,&m),(n||m))
      {
            memset(vis,0,sizeof(vis));
            memset(dis,0,sizeof(dis));
            memset(map,inf,sizeof(map));
            for(i=1;i<=m;i++)
            {
                 scanf("%d%d%d",&a,&b,&c);
                 map[a][b]=c;
                 map[b][a]=c;
            }
            dij(1,n);

      }return 0;



}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LDU分解是一种对数学矩阵进行分解的方法,它把一个矩阵分解为一个下三角矩阵L,一个对角矩阵D和一个上三角矩阵U的乘积。在计算机科学领域,CSDN是一个知名的技术社区,提供了大量的技术文章和教程。如果要用中文回答LDU分解CSDN,可以从以下几个方面来进行回答。 首先,可以简要介绍LDU分解的原理和应用。LDU分解是一种用来简化矩阵计算的方法,可以帮助我们更容易地理解和求解复杂的线性方程组。在实际应用中,LDU分解可以用于解决物理、工程、经济等领域的实际问题。 其次,可以探讨CSDN在技术领域的作用和影响。CSDN作为国内领先的IT技术社区,汇集了大量的技术人员和专家,为广大技术爱好者提供了学习、交流和分享的平台。在CSDN上,我们可以获取最新的技术动态、学习最新的编程语言和框架,并且还可以通过博客、问答等方式了解其他技术人员的经验和见解。 最后,可以谈谈如何利用CSDN平台获取关于LDU分解的相关知识。通过CSDN平台,我们可以搜索到大量关于LDU分解的文章、教程和讨论,可以通过阅读他人的经验和观点来更好地理解和应用LDU分解。同时,我们也可以在CSDN上发布自己的学习笔记和疑惑,与其他技术人员进行交流和讨论,共同进步。 总的来说,LDU分解和CSDN都在各自领域发挥着重要的作用,通过CSDN可以获取关于LDU分解的相关知识,从而更好地学习和应用这种数学方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值