无题
Description
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
2
3 2
1 2 50
2 3 10
3 1
1 2 40
Sample Output
Case 1: 60
Case 2: Impossible
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
struct Edge
{
int from,to,val,next;
}edge[2100];
int head[1010],edgenum;
void add(int u,int v,int w)
{
Edge E={u,v,w,head[u]};
edge[edgenum]=E;
head[u]=edgenum++;
}
int n;
int dis[110],vis[110];
void SPFA(int x)
{
queue<int>q;
memset(vis,0,sizeof(vis));
memset(dis,INF,sizeof(dis));
q.push(x);
dis[x]=0;
vis[x]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].val)
{
dis[v]=dis[u]+edge[i].val;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
if(dis[n]==INF)
printf("Impossible\n");
else
printf("%d\n",dis[n]);
}
int main(){
int t,m,s=0;
scanf("%d",&t);
while(t--)
{
int a,b,c;
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
edgenum=0;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
printf("Case %d: ",++s);
SPFA(1);
}
return 0;
}