hdu 2290 Find the Path(dij+heap)

来源:互联网 发布:冠新软件 中标 编辑:程序博客网 时间:2024/06/02 01:15

Find the Path

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 64768/64768 K (Java/Others)
Total Submission(s): 518    Accepted Submission(s): 167


Problem Description
Scofield is a hero in American show "Prison Break". He had broken the prison and started a big runaway.
Scofield has a map of US with cities and bidirectional roads between them. The lengths of roads are known. Some cities get a lot of cops who are very troublesome. Now Scofield needs your help to arrange his runaway route.

He needs a shortest path between two cities, while the quantity of the police in any city, except the start city and end city, on the route is no more than k.

You should know that it is very hard to escape. Scofield is very smart but not good at computer. Now Scofield is in trouble, can you help him with your computer? 
 

Input
The input consists of several test cases. There is an integer T on the first line indicating the number of test cases.
For each case, the first line consists of two integers N and M. N is the number of cities; M is the number of roads. The next line contains N integers C1, C2... CN, where Ci is the number of cops in city i.

Then followed M lines, each line consists of three integer, u, v, w, indicating there is a road with length w between city u and city v.

The following line consists of an integer Q, indicating the number of queries. Each of the following Q lines consists of three integers, u, v, k, indicating the query for the shortest path between city u and city v with limitation of k cops.

Technical Specification

1. T ≤ 20
2. 2 ≤ N ≤ 200, 0 ≤ M ≤ n * (n – 1) / 2
3. 0 ≤ Ci ≤ 1000,000,000
4. 0 ≤ u, v < N, 0 ≤ w ≤ 1000, 0 ≤ k ≤ 1000,000,000
5. 0 ≤ Q ≤ 100000
6. There is no more than ONE road between two cities and no road between the same cities.
7. For each query, u is not equal to v.
8. There is ONE empty line after each test case.
 

Output
For each query, output a single line contains the length of the shortest path. Output "-1" if you can't find the path. Please output an empty line after each test case.
 

Sample Input
14 4100 2 3 1000 1 10 2 11 3 22 3 320 3 20 3 1
 

Sample Output
3-1
 

Source
The 4th Baidu Cup final
 

Recommend
lcy

题解:高达10W次的查询竟然可以每次用dij+heap过....这科学么- -我再也不相信图论了


#include<stdio.h>#include<string.h>#include<queue>#define INF 0x3f3f3f3fusing namespace std;struct edge{    int next,data,len;}p[40005];int pre[205],dis[205],cos[205];int mark[205],cou,n;typedef pair<int,int>pii;void add(int x,int y,int z){    p[cou].next=pre[x];    p[cou].data=y;    p[cou].len=z;    pre[x]=cou++;}int dij(int x,int tail,int k){    priority_queue<pii,vector<pii>,greater<pii> >q;    int i,y;    for(i=0;i<n;i++) mark[i]=0,dis[i]=INF;    dis[x]=0;    q.push(pii(dis[x],x));    while(!q.empty())    {        pii now=q.top();        q.pop();        if(now.second==tail) break;        mark[now.second]=1;        for(i=pre[now.second];i!=-1;i=p[i].next)        {            y=p[i].data;            if(!mark[y]&&dis[y]>dis[now.second]+p[i].len&&(cos[y]<=k||y==tail))            {                dis[y]=dis[now.second]+p[i].len;                q.push(pii(dis[y],y));            }        }    }    return dis[tail]==INF?-1:dis[tail];}int main(){    int t,m,i,x,y,z,res;    //freopen("t.txt","r",stdin);    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        memset(pre,-1,sizeof(pre));        for(i=0;i<n;i++) scanf("%d",cos+i);        for(cou=i=0;i<m;i++)        {            scanf("%d%d%d",&x,&y,&z);            add(x,y,z);  add(y,x,z);        }        scanf("%d",&m);        for(i=0;i<m;i++)        {            scanf("%d%d%d",&x,&y,&z);            res=dij(x,y,z);            printf("%d\n",res);        }        printf("\n");    }    return 0;}


原创粉丝点击