河南省第十届ACM省赛题目:问题 I: Transmit information

来源:互联网 发布:ubuntu rm删除多个 编辑:程序博客网 时间:2024/06/07 03:49


问题 I: Transmit information

时间限制: 3 Sec  内存限制: 128 MB
提交: 2  解决: 2
[提交][状态][讨论版]
点击打开题目链接

题目描述

 The Chinese people threw themselves into an all-out war of resistance against Japanese aggression in 1937. The first line of  resistance against  aggression was formed by spies and underground workers. 

They lurked  in every place of the city.

 

There's a piece of information that needs to be passed on to them.  Now there is a  traffic map, each road connects two different intersectionsXiandYi, each of which is the termination for at least two road. The length of each road is known LENi,  no two intersections are directly connected by two different roads.

 

N spies lurk at every intersection , some intersections mignt have more than one spy. For security, they must position themselves properly ,each spy cannot accept information from localintersection,  can only be transferred from elsewhere and end up at the finishing pace.

 

At first, the information is in the hands of a spy atS intersection. AfterN spies transmission, and finally arrived atE intersection .

 

Write a program to find the shortest path that connects the starting intersection(S) and the ending intersection(E) ang transmission exactly N spies.

 

输入

The first line of the input contains one integers T, which is the nember of  test cases (1<=T<=5).  Each test case specifies:

 

* Line 1:        Four space-separated integers:  N  M  S  E

* Lines 2..M+1:  Line i+1 describes road i with three space-separated integers:  LENi  Xi  Yi

 

          (  2<=N<=300,000  2<=M<=100,  1<= LENi, Xi, Yi ,S, E <=1000  i=1,…,m)

 

输出

For each test case generate a single line containing a single integer that is the shortest path from intersection S to intersection E thattransmits exactly N spies.

样例输入

12  6  6  411  4  64  4  88  4  96  6  82  6  93  8  9

样例输出

10


题目意思:

给出一个N值,然后每个节点都有N个间谍,题目说了为了安全起见,一个点的间谍只能从其他点的间谍处接受消息,然后给出M条道路,给出起点S,终点E。

求通过N个间谍的传递,消息从S传到E的最短路径。

解题思路:

用矩阵的思想写.和矩阵快速幂很像,就是幂是求乘,这个是求矩阵加法。比赛前看到过一样的题目,当时就随便看了一下,并没有去好好看,去写,所以比赛

也没看出来是原题,炒鸡难过。


#include<iostream>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<map>#define inf 0x3f3f3f3fusing namespace std;typedef long long ll;const int maxn = 110;int N,M,S,E;   ///经过N个间谍传播,M道路条数,struct matrix{    ll a[maxn][maxn];}dis,ans;matrix flody(matrix A,matrix B,int n){    matrix tmp;    for(int i = 1; i < n; i++)        for(int j = 1; j < n; j++)            tmp.a[i][j] = inf;    for(int k = 1; k < n; k++)        for(int i = 1; i < n; i++)            for(int j = 1; j < n; j++)                if(tmp.a[i][j]>A.a[i][k]+B.a[k][j])                    tmp.a[i][j] = A.a[i][k] + B.a[k][j];    return tmp;}void solve(int n,int s,int e){    for(int i = 1; i < n; i++)        for(int j = 1; j < n; j++)            ans.a[i][j] = dis.a[i][j];    N--;      while(N)       {        if(N&1)           {            ans = flody(ans,dis,n);        }        dis = flody(dis,dis,n);        N = N>>1;    }    if(ans.a[s][e] < inf)        printf("%lld\n",ans.a[s][e]);}int main(){    int T,len,x,y;    scanf("%d",&T);    while(T--)    {        map<int,int>m;  ///用map来给点进行编号。        int num = 1;        scanf("%d%d%d%d",&N,&M,&S,&E);        for(int i = 0; i < maxn; i++)            for(int j = 0; j < maxn; j++)                dis.a[i][j] = inf;         for(int i = 1; i <= M; i++)        {            scanf("%d%d%d",&len,&x,&y);            if(m[x]==0)            {                m[x] = num++;            }            int xi = m[x];            if(m[y]==0)            {                m[y] = num++;            }            int yi = m[y];            if(len < dis.a[xi][yi])                dis.a[xi][yi] = dis.a[yi][xi] = len;        }        solve(num,m[S],m[E]);    }    return 0;}



阅读全文
0 0
原创粉丝点击