Lightoj 1019 - Brush (V)【dijkstra+spfa】

1019 - Brush (V)
    PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

Tanvir returned home from the contest and got angry after seeing his room dusty. Who likes to see a dusty room after a brain storming programming contest? After checking a bit he found that there is no brush in him room. So, he called Atiq to get a brush. But as usual Atiq refused to come. So, Tanvir decided to go to Atiq's house.

The city they live in is divided by some junctions. The junctions are connected by two way roads. They live in different junctions. And they can go to one junction to other by using the roads only.

Now you are given the map of the city and the distances of the roads. You have to find the minimum distance Tanvir has to travel to reach Atiq's house.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a blank line. The next line contains two integers N (2 ≤ N ≤ 100) and M (0 ≤ M ≤ 1000), means that there are N junctions and M two way roads. Each of the next M lines will contain three integers u v w (1 ≤ u, v ≤ N, w ≤ 1000), it means that there is a road between junction u and v and the distance is w. You can assume that Tanvir lives in the 1st junction and Atiq lives in the Nth junction. There can be multiple roads between same pair of junctions.

Output

For each case print the case number and the minimum distance Tanvir has to travel to reach Atiq's house. If it's impossible, then print 'Impossible'.

Sample Input

Output for Sample Input

2

 

3 2

1 2 50

2 3 10

 

3 1

1 2 40

Case 1: 60

Case 2: Impossible

 

题意:
起点为1 ,终点为n ,给出m条路,求最短路,如果不存在,输出提示

题解:

很久没做基础图论了,就练练感觉了....
废话少说,上代码:


裸的dijkstra:

/*
http://blog.csdn.net/liuke19950717
*/
#include
#include
#include
using namespace std;
const int maxn=105;
const int inf=0x3f3f3f3f;
int graph[maxn][maxn];
void init(int n)
{
    memset(graph,inf,sizeof(graph));
    for(int i=1;i<=n;++i)
    {
        graph[i][i]=0;
    }
}
int dijkstra(int n,int st)
{
    int vis[maxn]={0},dist[maxn];
    memset(dist,inf,sizeof(dist));
    dist[st]=0;
    while(1)
    {
        int k=-1;
        for(int i=1;i<=n;++i)
        {
            if(!vis[i]&&(k==-1||dist[i]



spfa:


/*
http://blog.csdn.net/liuke19950717
*/
#include
#include
#include
#include
using namespace std;
const int maxn=105;
const int inf=0x3f3f3f3f;
int graph[maxn][maxn];
void init(int n)
{
    memset(graph,inf,sizeof(graph));
    for(int i=1;i<=n;++i)
    {
        graph[i][i]=0;
    }
}
int spfa(int n,int st)
{
    int vis[maxn]={0},dist[maxn];
    memset(dist,inf,sizeof(dist));
    queue q;
    q.push(st);
    vis[st]=1;dist[st]=0;
    while(!q.empty())
    {
        int k=q.front();q.pop();
        vis[k]=0;
        for(int i=1;i<=n;++i)
        {
            if(dist[i]>dist[k]+graph[k][i])
            {
                dist[i]=dist[k]+graph[k][i];
                if(!vis[i])
                {
                    vis[i]=1;
                    q.push(i);
                }
            }
        }
    }
    return dist[n];
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int k=1;k<=t;++k)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        init(n);
        for(int i=0;i



基于邻接表的两种方法:


spfa:

/*
http://blog.csdn.net/liuke19950717
*/
#include
#include
#include
#include
using namespace std;
const int maxn=105;
const int inf=0x3f3f3f3f;
int edgenum,head[maxn];
struct node
{
    int to,val,next;
    bool friend operator < (node a,node b)
    {
        return a.val>b.val;
    }
}edge[maxn*maxn];
void init(int n)
{
    edgenum=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
    node tp={v,w,head[u]};
    edge[edgenum]=tp;
    head[u]=edgenum++;
}
int spfa(int n,int st)
{
    int vis[maxn]={0},dist[maxn];
    memset(dist,inf,sizeof(dist));
    queue q;
    q.push(st);
    dist[st]=0;vis[st]=1;
    while(!q.empty())
    {
        int k=q.front();q.pop();
        vis[k]=0;
        for(int i=head[k];i!=-1;i=edge[i].next)
        {
            int tp=edge[i].to;
            if(dist[tp]>dist[k]+edge[i].val)
            {
                dist[tp]=dist[k]+edge[i].val;
                if(!vis[tp])
                {
                    vis[tp]=1;
                    q.push(tp);
                }
            }
        }
    }
    return dist[n];
}
 
int main()
{
    int t;
    scanf("%d",&t);
    for(int k=1;k<=t;++k)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        init(n);
        for(int i=0;i




优先队列优化的dijkstra:

原来一直以来自己理解的有问题,今天才真正实现了这个算法...


/*
http://blog.csdn.net/liuke19950717
*/
#include
#include
#include
#include
using namespace std;
const int maxn=105;
const int inf=0x3f3f3f3f;
int edgenum,head[maxn];
struct node
{
    int to,val,next;
}edge[maxn*maxn];
struct heapnode
{
    int ver,dis;
    bool friend operator < (heapnode a,heapnode b)
    {
        return a.dis>b.dis;
    }
};
void init(int n)
{
    edgenum=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
    node tp={v,w,head[u]};
    edge[edgenum]=tp;
    head[u]=edgenum++;
}
 
int dijkstra(int n,int st)
{
    int dist[maxn];
    memset(dist,inf,sizeof(dist));
    priority_queue q;
    q.push((heapnode){st,0});
    dist[st]=0;
    while(!q.empty())
    {
        heapnode tp=q.top();q.pop();
        int k=tp.ver;
        for(int i=head[k];i!=-1;i=edge[i].next)
        {
            if(dist[edge[i].to]>dist[k]+edge[i].val)
            {
                dist[edge[i].to]=dist[k]+edge[i].val;
                q.push((heapnode){edge[i].to,dist[edge[i].to]});
            }
        }
    }
    return dist[n];
}
 
int main()
{
    int t;
    scanf("%d",&t);
    for(int k=1;k<=t;++k)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        init(n);
        for(int i=0;i


你可能感兴趣的:(其他oj,最短路&&k短路)